Jump to content

forms with php


pugs1501

Recommended Posts

I am creating a form in PHP and I can get most of it to work. I am just having problems with the check box feature. I want to be able to have a check box that the user will select multiple items and then put them all into one variable so when I do a Mysql call looking for one of the items it will show me that user chose it. Here is just a test form I am just trying to get the Functionality working.

 

<?php

if ($_GET['catupdate'] == 'Update'){

 

$pets1 = $_GET[pets]." ".$_GET[pets]." ".$_GET[pets];

echo $_GET['catname'];

echo "<br>\n";

echo $_GET['catrank'];

echo "<br>\n";

echo $_GET['catint'];

echo "<br>\n";

echo $pets1;

 

}else{

echo "<center><table border=0 cellpadding=0 cellspacing=0 width=90%>";

echo "<form method=get action=index.php>";

echo "<tr><td><font color=000000>Name: </font><input type=Text size=40 name=catname></td></tr>";

echo "<tr><td><font color=000000>Rank: </font><input type=Text size=40 name=catrank></td></tr>";

echo "<tr><td><font color=000000>Initial: </font><input type=Text size=15 name=catint></td></tr>";

echo "<tr><td><font color=000000>pets: </font><input type=checkbox name=pets value=dog> dog</td></tr>";

echo "<tr><td><font color=000000>pets: </font><input type=checkbox name=pets value=cat> cat</td></tr>";

echo "<tr><td><font color=000000>pets: </font><input type=checkbox name=pets value=rat> rat</td></tr>";

echo "<tr><td><center><input type=Submit name=catupdate value=Update></center></td></tr>";

echo "</form></table></center>";

 

}

 

?>

 

 

here is what the result looks like

 

testdata    // catname variable

2              // catrank variable

example    // catint Variable

rat rat rat  // pets variable with a space

 

 

I would just make the name of the check boxes all different but when I am pulling from a database for the selections I am not sure how many I would have. Any help would be very much appreciated.

 

 

 

 

Link to comment
Share on other sites

If you want to get multiple values back from a checkbox, you need to make the name an array. Also you should always quote all values of all attributes.

 

<?php
   echo "<form method='get' action='index.php'>";
   echo "<tr><td><font color='000000'>Name: </font><input type='Text' size='40' name='catname'></td></tr>";
   echo "<tr><td><font color='000000'>Rank: </font><input type='Text' size='40' name='catrank'></td></tr>";
   echo "<tr><td><font color='000000'>Initial: </font><input type='Text' size='15' name='catint'></td></tr>";
   echo "<tr><td><font color='000000'>pets: </font><input type='checkbox' name='pets[]' value='dog'> dog</td></tr>";
   echo "<tr><td><font color='000000'>pets: </font><input type='checkbox' name='pets[]' value='cat'> cat</td></tr>";
   echo "<tr><td><font color='000000'>pets: </font><input type='checkbox' name='pets[]' value='rat'> rat</td></tr>";
   echo "<tr><td><center><input type='Submit' name='catupdate' value='Update'></center></td></tr>";
?>

 

Then in your processing, you would do:

<?php
f ($_GET['catupdate'] == 'Update'){
   
   $pets1 = implode(' ',$_GET['pets]');
   echo $_GET['catname'];
   echo "<br>";
   echo $_GET['catrank'];
   echo "<br>";
   echo $_GET['catint'];
   echo "<br>";
   echo $pets1;
?>

 

Ken

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.