PHP+HTML Form Help

  1. <?php
  2. if (!empty($_POST['AI']))
  3.     {
  4.         add_item($_POST['itemName'], $_POST['itemType'], $_POST['itemIcon']);
  5.     };
  6.     if (!empty($_POST['RI']))
  7.     {
  8.         remove_item($_POST['itemName'], $_POST['itemType']);
  9.     };
  10.     if (!empty($_POST['UI']))
  11.     {
  12.         update_item($_POST['itemID'], $_POST['itemName'], $_POST['itemType'], $_POST['itemIcon']);
  13.     };
  14.        
  15.        
  16.        
  17. ?>
  18.  
  19. <div id="form"><form action="<?php echo $PHP_SELF;?>" method="post"><br />
  20. Item ID: <input id="itemID" type="text" size="5" /><br />
  21. Item Name:  <input id="itemName" type="text" /><br />
  22. Item Type:  <input id="itemType" type="text" /><br />
  23. Item Icon Name:  <input id="itemIcon" type="text"></div>
  24. <br /><br />
  25.     <input id="itemAdd" type="submit" value="Add Item" name="AI" />
  26.     <input id="itemRemove" type="submit" value="Remove Item" name="RI" />
  27.     <input id="itemUpdate" type="submit" value="Update Item" name="UI" /></form></div>
  28.  
Expand to see the code.


Why won't the above work for me? The phpFunctions work just fine, but they don't seem to be getting called...
$PHP_SELF should be $_SERVER['PHP_SELF'], unless you manually set $PHP_SELF your...self.

How are you verifying that it's not working? Maybe it's the code in the other functions?

EDIT: Also, for your HTML, you should switch the opening <div> tag and the opening <form> tag.
Actually, it looks to me like he just has an extra closing tag for the div. He's got 2 closing and only one opening. He should take out the first closing tag on line 23.
Even so, it's good HTML style to have

<b><i>Text</i></b>


and not

<b><i>Text</b></i>


Either the order of the opening tags or the order of the closing tags should be switched. It won't matter in rendering, but it is invalid, and it's not good style (and therefore potentially confusing to others... not necessarily in this case but it more complex ones).
Right, but if he takes out the closing tag on 23, he has <div><form></form></div> which is OK
...ah. D'oh.
If you look, I have 3 submit buttons. None of them do anything. The tut i followed on using more than 1 submit button said to check if their name is posted... basically I'm trying to have it tell me which button is being pressed, then preform the function specified. but it doesn't do anything but reload without adding anything to the database...

the 'extra' closes one not shown. it is my wrapper class... I wasn't worried about validating my code right now. Especally because IE doesn't like 'perfect' pages.
Nah, what you want to do is use an OnClick function, and use JavaScript to change a hidden field to denote which button was clicked. So your OnCLick for each button will change that hidden field and then submit the form.
... I hate Java... :P I really should learn it... What exactly would I have to do? Javawise that is. Hidden textboxes are easy...
DeM0nFiRe wrote:
Nah, what you want to do is use an OnClick function, and use JavaScript to change a hidden field to denote which button was clicked. So your OnCLick for each button will change that hidden field and then submit the form.


Why do this? Whats the point? PHP can do it fine.

To do this:
Change your buttons to have all the same names but different values.

Like this:
  1. <?php
  2.  
  3. //The magic:
  4.  
  5. if($_POST['button'] == "Add Item"){
  6.  
  7.  
  8.         add_item($_POST['itemName'], $_POST['itemType'], $_POST['itemIcon']);
  9.  
  10. }elseif($_POST['button'] == "Remove Item"){
  11.  
  12.          remove_item($_POST['itemName'], $_POST['itemType']);
  13.  
  14. }elseif($_POST['button'] == "Update Item"){
  15.  
  16.         update_item($_POST['itemID'], $_POST['itemName'], $_POST['itemType'], $_POST['itemIcon']);
  17. };
  18.        
  19.        
  20.        
  21. ?>
  22.  
  23. <div id="form"><form action="<?php echo $PHP_SELF;?>" method="post"><br />
  24. Item ID: <input id="itemID" type="text" size="5" /><br />
  25. Item Name:  <input id="itemName" type="text" /><br />
  26. Item Type:  <input id="itemType" type="text" /><br />
  27. Item Icon Name:  <input id="itemIcon" type="text"></div>
  28. <br /><br />
  29.     <input id="itemAdd" type="submit" value="Add Item" name="button" />
  30.     <input id="itemRemove" type="submit" value="Remove Item" name="button" />
  31.     <input id="itemUpdate" type="submit" value="Update Item" name="button" /></form></div>
  32.  
Expand to see the code.
I'm still curious, though, because the way he did it should have worked (regardless of whether or not it was ideal). Try replacing those function calls with echo/print statements, just to verify that this works. I think the problem was in your actual functions (add_item(), remove_item(), and update_item()), not the code you've posted here.
No, they function perfectly. calling the functions without the form works fine.
It should work. However I've just noticed that you never set the name attribute of itemId, itemType, itemName, or itemIcon, you've just set the id attribute. You need to set the name tag if you want those values to actually be passed in.
Ceroscuro wrote:
It should work. However I've just noticed that you never set the name attribute of itemId, itemType, itemName, or itemIcon, you've just set the id attribute. You need to set the name tag if you want those values to actually be passed in.

Whoa, can't believe it didnt see that. :( /shame

This should work:
  1. <?php
  2.  
  3. //The magic:
  4.  
  5. if($_POST['button'] == "Add Item"){
  6.  
  7.  
  8.         add_item($_POST['itemName'], $_POST['itemType'], $_POST['itemIcon']);
  9.  
  10. }elseif($_POST['button'] == "Remove Item"){
  11.  
  12.          remove_item($_POST['itemName'], $_POST['itemType']);
  13.  
  14. }elseif($_POST['button'] == "Update Item"){
  15.  
  16.         update_item($_POST['itemID'], $_POST['itemName'], $_POST['itemType'], $_POST['itemIcon']);
  17. };
  18.        
  19.        
  20.        
  21. ?>
  22.  
  23. <div id="form"><form action="<?php echo $PHP_SELF;?>" method="post"><br />
  24. Item ID: <input id="itemID" type="text" size="5" name="itemID" /><br />
  25. Item Name:  <input id="itemName" type="text" name="itemName" /><br />
  26. Item Type:  <input id="itemType" type="text" name="itemType" /><br />
  27. Item Icon Name:  <input id="itemIcon" type="text" name="itemIcon"></div>
  28. <br /><br />
  29.     <input id="itemAdd" type="submit" value="Add Item" name="button" />
  30.     <input id="itemRemove" type="submit" value="Remove Item" name="button" />
  31.     <input id="itemUpdate" type="submit" value="Update Item" name="button" /></form></div>
  32.  
Expand to see the code.
I missed it at first too; I think I was looking for an error in the PHP and that's why I missed it.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=66146