Jump to content

Button Functions


absorr

Recommended Posts

I know that in JavaScript you use onClick="functionName" but how do you do it in a php file? I have HTML echoed in a php and I want it so that when the button is pressed in the HTML that is echoed it will start some kind of function. How do I do that?

Link to comment
Share on other sites

PHP can't do that. That's what Javascript is for.
Well I want it so that when you insert a player's name and after it gives you information about that player that you can do things like ban, kick, give an item, etc, but the function requires a name input and I don't want to have to put the name in all over again. I need some way that the received name is carried throughout.
Link to comment
Share on other sites

PHP can't act upon clicks.But forms can be submitted (see the forms part of the HTML tutorial), and PHP can process the data from (see the forms part of the PHP tutorial).

Link to comment
Share on other sites

PHP can't act upon clicks. But forms can be submitted (see the forms part of the HTML tutorial), and PHP can process the data from (see the forms part of the PHP tutorial).
I know that already, how else do you think it got the information in the first place? I have made tons pf HTML forums that link to PHP but I want to know how I can make it so that after that 1st one, previous inputs are recycled in the next part. The way I was it the easiest way would seem to be a forum in the PHP file that will activate a part in the same PHP file so that it never loses the information. How do I do this?
Link to comment
Share on other sites

I know that already, how else do you think it got the information in the first place?
Sorry... we're touchy about terminology, because when dealing with complex problems, it's often the use of wrong/misleading terminology that leads us into providing useless/irrelevant solutions => proper terminology = quick, useful, relevant and proper solutions.
... HTML forums ...
See, that's another term that may lead many people into giving you more "advanced" kind of responses than what you're really asking for. The term is "HTML forms". "Forums" are whole large applications, such as the one we're writing in right now. The only reason I'm able to detect that's not what you mean is that we are in a forum... if you were asking this in a mailing list, I might have fallen for the trap and ask you something like "If you've made forums, you should know this already...".You can use PHP to generate <input type="hidden" /> fields that contain the previous information in their name and value attributes, or you could use the GET method while setting the form's action to the current URL (thus embedding the previous data into the URL), or you could start a session after the first call. Each approach has its benefits and drawbacks:1. hidden inputs are not preserved in case the browser is closed (i.e. closing the browser = start the whole form over again), but if the user decided to take a break in the middle of the form, he could leave the browser open and continue when ready. Also, each next page loads slower since the old data is downloaded in addition to the new form.2. GET method with action setting is not preserved (as in hidden inputs), but if the user is wise enough to copy the current page's URL before closing their browser, and paste it back later, they can resume from where they left. Data in the URL is generally limited though, so it should be used for stuff other than large text boxes, such as the one for this post. Page will generally load just as fast on all pages.3. A session is saved at each page, so users can resume even if they don't copy&paste the URL, storage size per user is not an issue, and requests don't grow as you advance pages. If they stop filling in the form though, they must return within a time limit that you can adjust, but must have one non the less. The longer time you specify, the higher the risk that your web server will run out of space (because it is where the session data of each user is stored, until you're actually ready to submit it to your database or whatever).A mixture of these would be ideal, but as far as simplicity goes - hidden inputs all the way.
Link to comment
Share on other sites

Sorry... we're touchy about terminology, because when dealing with complex problems, it's often the use of wrong/misleading terminology that leads us into providing useless/irrelevant solutions => proper terminology = quick, useful, relevant and proper solutions. See, that's another term that may lead many people into giving you more "advanced" kind of responses than what you're really asking for. The term is "HTML forms". "Forums" are whole large applications, such as the one we're writing in right now. The only reason I'm able to detect that's not what you mean is that we are in a forum... if you were asking this in a mailing list, I might have fallen for the trap and ask you something like "If you've made forums, you should know this already...". You can use PHP to generate <input type="hidden" /> fields that contain the previous information in their name and value attributes, or you could use the GET method while setting the form's action to the current URL (thus embedding the previous data into the URL), or you could start a session after the first call. Each approach has its benefits and drawbacks: 1. hidden inputs are not preserved in case the browser is closed (i.e. closing the browser = start the whole form over again), but if the user decided to take a break in the middle of the form, he could leave the browser open and continue when ready. Also, each next page loads slower since the old data is downloaded in addition to the new form.2. GET method with action setting is not preserved (as in hidden inputs), but if the user is wise enough to copy the current page's URL before closing their browser, and paste it back later, they can resume from where they left. Data in the URL is generally limited though, so it should be used for stuff other than large text boxes, such as the one for this post. Page will generally load just as fast on all pages.3. A session is saved at each page, so users can resume even if they don't copy&paste the URL, storage size per user is not an issue, and requests don't grow as you advance pages. If they stop filling in the form though, they must return within a time limit that you can adjust, but must have one non the less. The longer time you specify, the higher the risk that your web server will run out of space (because it is where the session data of each user is stored, until you're actually ready to submit it to your database or whatever). A mixture of these would be ideal, but as far as simplicity goes - hidden inputs all the way.
YES! A hidden input! THANK YOU! That was exactly what I was looking for! THANK YOU THANK YOU THANK YOU! Now to see if works.... EDIT It does!
Link to comment
Share on other sites

So that I don't have to start a new topic, I'll just tell you my related problem. I have 10 hearts next to each other to show the player's health level. I want to make it so that if you were to click on the last heart, it will, using PHP, carry on the player name with the health level 20 to the PHP file. Very similar to my above problem only I want it when you click a picture instead of pressing a button to submit the forum. Now that I think about it, this probably fits under the HTML category better. Oh well, it does include PHP. So how do I make this happen?

Link to comment
Share on other sites

Read the <input /> element's page from top to bottom (OK, OK... the attributes part I mean) without skipping anything, and you'll see it. Alternatively, you could use the <button /> element with an <img /> in it, but keep in mind this doesn't work in IE versions prior to IE8.

Link to comment
Share on other sites

Read the <input /> element's page from top to bottom (OK, OK... the attributes part I mean) without skipping anything, and you'll see it. Alternatively, you could use the <button /> element with an <img /> in it, but keep in mind this doesn't work in IE versions prior to IE8.
That did not work. I click on the image button (using chrome. also tried on IE9) and it does nothing. I used the button tag because looking at the input it was an image not an image button.Here's the code
<?phprequire('JSONAPI.php'); // get this file at: https://github.com/alecgorge/jsonapi/raw/master/sdk/php/JSONAPI.php$api = new JSONAPI(CENSORED, CENSORED, CENSORED, CENSORED, CENSORED);echo "<form action=\"setPlayerHealth.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"hidden\" name=\"heart\" value=\"2\" /><button type=\"button\"><img src=\"http://www.minecraftwiki.net/images/thumb/a/a7/Heart.svg/9px-Heart.svg.png\"></button></forum><form action=\"setPlayerHealth.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"hidden\" name=\"heart\" value=\"4\" /><button type=\"button\"><img src=\"http://www.minecraftwiki.net/images/thumb/a/a7/Heart.svg/9px-Heart.svg.png\"></button></forum><form action=\"setPlayerHealth.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"hidden\" name=\"heart\" value=\"6\" /><button type=\"button\"><img src=\"http://www.minecraftwiki.net/images/thumb/a/a7/Heart.svg/9px-Heart.svg.png\"></button></forum><form action=\"setPlayerHealth.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"hidden\" name=\"heart\" value=\"8\" /><button type=\"button\"><img src=\"http://www.minecraftwiki.net/images/thumb/a/a7/Heart.svg/9px-Heart.svg.png\"></button></forum><form action=\"setPlayerHealth.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"hidden\" name=\"heart\" value=\"10\" /><button type=\"button\"><img src=\"http://www.minecraftwiki.net/images/thumb/a/a7/Heart.svg/9px-Heart.svg.png\"></button></forum><form action=\"setPlayerHealth.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"hidden\" name=\"heart\" value=\"12\" /><button type=\"button\"><img src=\"http://www.minecraftwiki.net/images/thumb/a/a7/Heart.svg/9px-Heart.svg.png\"></button></forum><form action=\"setPlayerHealth.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"hidden\" name=\"heart\" value=\"14\" /><button type=\"button\"><img src=\"http://www.minecraftwiki.net/images/thumb/a/a7/Heart.svg/9px-Heart.svg.png\"></button></forum><form action=\"setPlayerHealth.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"hidden\" name=\"heart\" value=\"16\" /><button type=\"button\"><img src=\"http://www.minecraftwiki.net/images/thumb/a/a7/Heart.svg/9px-Heart.svg.png\"></button></forum><form action=\"setPlayerHealth.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"hidden\" name=\"heart\" value=\"18\" /><button type=\"button\"><img src=\"http://www.minecraftwiki.net/images/thumb/a/a7/Heart.svg/9px-Heart.svg.png\"></button></forum><form action=\"setPlayerHealth.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"hidden\" name=\"heart\" value=\"20\" /><button type=\"button\"><img src=\"http://www.minecraftwiki.net/images/thumb/a/a7/Heart.svg/9px-Heart.svg.png\"></button></forum>";echo "<br><br><form action=\"ban.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"submit\" value=\"Ban\"/></form>";echo "<form action=\"unban.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"submit\" value=\"Unban\"/></form>";echo "<form action=\"banIP.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"submit\" value=\"Ban IP\"/></form>";echo "<form action=\"unbanIP.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"submit\" value=\"UnbanIP\"/></form>";echo "<form action=\"opPlayer.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"submit\" value=\"Make Op\"/></form>";echo "<form action=\"unopPlayer.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" /><input type=\"submit\" value=\"Unop\"/></form>";echo "<form action=\"kickPlayer.php\" method=\"post\"><input type=\"hidden\" name=\"name\" value=\"{$_POST['name']}\" />Reason: <input type=\"text\" name=\"msg\" /><input type=\"submit\" value=\"Kick\"/></form>";var_dump($api->call("getPlayer", array($_POST["name"])));?>

Link to comment
Share on other sites

Next time, consider making your HTML more readable (for some reason that's beyond me, many people forget doing that)... you can, for example, convert all echoes into a temporary turn off, or use a template engine.In the case of a turn off, that would look something like:

<?phprequire('JSONAPI.php'); // get this file at: https://github.com/alecgorge/jsonapi/raw/master/sdk/php/JSONAPI.php$api = new JSONAPI(CENSORED, CENSORED, CENSORED, CENSORED, CENSORED);?><form action="setPlayerHealth.php" method="post"><input type="hidden" name="name" value="<?php echo $_POST['name']; ?>" /><input type="hidden" name="heart" value="2" /><button type="submit"><img src="http://www.minecraftwiki.net/images/thumb/a/a7/Heart.svg/9px-Heart.svg.png"></button></form>...

BTW, you keep misspelling the word "form" as "forum"... even in your code.

Link to comment
Share on other sites

I'm not having this argument with you. Thanks for helping.
:lol: what's even the argument? It's a <form> tag, not a <forum> tag.. (you've are using </forum> closing tags..)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...