Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

PHP+HTML Form Help

Tdata

Sponsor

PHP:
<div class="php" id="{CB}" style="font-family: monospace;"><ol><span style="color: #000000; font-weight: bold;"><?php

[url=http://www.php.net/if]<span style="color: #b1b100;">if[/url] (!empty($_POST['AI']))

    {

        add_item($_POST['itemName'], $_POST['itemType'], $_POST['itemIcon']);

    };

    [url=http://www.php.net/if]<span style="color: #b1b100;">if[/url] (!empty($_POST['RI']))

    {

        remove_item($_POST['itemName'], $_POST['itemType']); 

    };

    [url=http://www.php.net/if]<span style="color: #b1b100;">if[/url] (!empty($_POST['UI']))

    {

        update_item($_POST['itemID'], $_POST['itemName'], $_POST['itemType'], $_POST['itemIcon']); 

    };

        

        

        

<span style="color: #000000; font-weight: bold;">?>

 

<div id="form"><form action="<?php echo $PHP_SELF;?>" method="post"><br />

<span style="color: #000000; font-weight: bold;">Item ID: <input id="itemID" type="text" size="5" /><br />

<span style="color: #000000; font-weight: bold;">Item Name:  <input id="itemName" type="text" /><br />

<span style="color: #000000; font-weight: bold;">Item Type:  <input id="itemType" type="text" /><br />

<span style="color: #000000; font-weight: bold;">Item Icon Name:  <input id="itemIcon" type="text"></div>

<br /><br />

    <input id="itemAdd" type="submit" value="Add Item" name="AI" />

    <input id="itemRemove" type="submit" value="Remove Item" name="RI" />

    <input id="itemUpdate" type="submit" value="Update Item" name="UI" /></form></div>

 

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).
 

Tdata

Sponsor

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.
 

Tdata

Sponsor

... I hate Java... :P I really should learn it... What exactly would I have to do? Javawise that is. Hidden textboxes are easy...
 

Akura

Member

DeM0nFiRe":36i79ni7 said:
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:
Code:
<?php

 

//The magic:

 

if($_POST['button'] == "Add Item"){

 

 

        add_item($_POST['itemName'], $_POST['itemType'], $_POST['itemIcon']);

 

}elseif($_POST['button'] == "Remove Item"){

 

         remove_item($_POST['itemName'], $_POST['itemType']);

 

}elseif($_POST['button'] == "Update Item"){

 

        update_item($_POST['itemID'], $_POST['itemName'], $_POST['itemType'], $_POST['itemIcon']);

};

       

       

       

?>

 

<div id="form"><form action="<?php echo $PHP_SELF;?>" method="post"><br />

Item ID: <input id="itemID" type="text" size="5" /><br />

Item Name:  <input id="itemName" type="text" /><br />

Item Type:  <input id="itemType" type="text" /><br />

Item Icon Name:  <input id="itemIcon" type="text"></div>

<br /><br />

    <input id="itemAdd" type="submit" value="Add Item" name="button" />

    <input id="itemRemove" type="submit" value="Remove Item" name="button" />

    <input id="itemUpdate" type="submit" value="Update Item" name="button" /></form></div>

 
 
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.
 
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.
 

Akura

Member

Ceroscuro":10ogmncl said:
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:
Code:
<?php

 

//The magic:

 

if($_POST['button'] == "Add Item"){

 

 

        add_item($_POST['itemName'], $_POST['itemType'], $_POST['itemIcon']);

 

}elseif($_POST['button'] == "Remove Item"){

 

         remove_item($_POST['itemName'], $_POST['itemType']);

 

}elseif($_POST['button'] == "Update Item"){

 

        update_item($_POST['itemID'], $_POST['itemName'], $_POST['itemType'], $_POST['itemIcon']);

};

       

       

       

?>

 

<div id="form"><form action="<?php echo $PHP_SELF;?>" method="post"><br />

Item ID: <input id="itemID" type="text" size="5" name="itemID" /><br />

Item Name:  <input id="itemName" type="text" name="itemName" /><br />

Item Type:  <input id="itemType" type="text" name="itemType" /><br />

Item Icon Name:  <input id="itemIcon" type="text" name="itemIcon"></div>

<br /><br />

    <input id="itemAdd" type="submit" value="Add Item" name="button" />

    <input id="itemRemove" type="submit" value="Remove Item" name="button" />

    <input id="itemUpdate" type="submit" value="Update Item" name="button" /></form></div>

 
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top