Page 1 of 1

Getting Data from Forms

Posted: Thu Dec 11, 2003 1:22 pm
by Dowfen
Hello,

First, I just want you to know I'm a PHP newbie. I know Java so the classes make all kinds of sense, but I'm trying to do some very simple things right now.

What I like'd to do is take a form that involves checkboxes and get data from that to make queries into MySQL (specifically, delete rows based on the data from the form).

So, I was doing this last night and was failing, but that's to be expected as it was late.

The data that fills the form is from a MySQL query that I think is right. Here is what the form looks like when viewing the source:

<input type=checkbox name="Test">Test</input>

I know it does output something (I tried it with a formmail script) and it sends something like:

Test: On

My form action and submit button looks like:

<form action="deletelinks.php" method="post">
<input type=submit name=deletelinks value="Delete">

And my problems come with the "deletelinks.php" file. What should be in this to get the form data into PHP variables.

Right now the "deletelinks.php" page is basically blank. But I was trying stuff like:

$formsubmittedata = $_REQUEST['name'];

I'm sorry if this is very elementary, I've done very little work with PHP, but so far I really like it.

If you're wondering, this is my entire deletelinks.php file (print call was for testing purposes):

<?

$formVariableName = $_REQUEST['name'];

$recipient = $formVariableName;

print($recipient);

?>

Thank you very much for any help in advance,

Eric

Posted: Thu Dec 11, 2003 1:49 pm
by McGruff
It's often useful to try this when debugging a form submission script:

Code: Select all

<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>
Try a forum search for more info on using checkboxes.

Posted: Thu Dec 11, 2003 1:59 pm
by microthick
You still need to give a value to the input checkbox.

So your html might look like:

<input type="checkbox" name="mycheckbox" value="on">

Then, if there is a checkmark in that box, on the action page $_POST["mycheckbox"] will contain "on".

If there is no checkmark, then $_POST["mycheckbox"] will be unset.

Posted: Thu Dec 11, 2003 2:12 pm
by AnsonM
If you use POST or GET, then on the next page, you can display the info... or do stuff like send emails with it...

e.g. for post u could have:

Code: Select all

<? $name=$HTTP_POST_VARS['name']; ?>
$HTTP_POST_VARS['name']; , meaning the field name from the form, and $name= being the variable...

Posted: Thu Dec 11, 2003 2:19 pm
by Dowfen
So basically the checkboxes aren't going to work for me because I'll need the name to do anything usefully with MySQL.

Right?

By the way, thanks for the replies so far. They're really helping me!

Posted: Thu Dec 11, 2003 2:32 pm
by AnsonM
For the checkboxes...

You just do the normal thing with them in the form..

e.g:

Code: Select all

<? $gender=$HTTP_POST_VARS['gender']; ?>
So if the form returned Male, then $gender would be Male, and if they chose female, then $gender would = female...

Posted: Thu Dec 11, 2003 2:38 pm
by Draco_03
you need to specify a value for the checkbox...

peace

Posted: Thu Dec 11, 2003 3:22 pm
by AnsonM
Yeah, so you have to set the value from the form...

Posted: Fri Dec 12, 2003 12:55 am
by Dowfen
Again, I want to let everyone know I greatly appreciate all the help.

Althought, something (probably painfully obvious) eludes me. I've been trying some testing, and nothing has been working. I guess the hint lies in why this doesn't work:

------------------------------test.php-------------------------------------

Code: Select all

<form action="page.php" method="post"> 
<?
	
		print("<input type=radio name="test" . "" . variable="test" . "">" . test . 

"</input>");

?>
<input type="submit"> 
</form>

--------------------------------page.php---------------------------

Code: Select all

<?php 
echo $_POST&#1111;'variable']; 
?>
This is sort of what doesn't work for me. The variable isn't getting defined as nothing comes up on the screen. I know it's probably obvious, but I'm missing it.

Thanks in advance,

Eric

Posted: Fri Dec 12, 2003 1:05 am
by microthick

Code: Select all

<form action="page.php" method="post"> 
<? 
    
      print("<input type="radio" name="test" value="test">"); 

?> 
<input type="submit"> 
</form>

Posted: Fri Dec 12, 2003 1:13 am
by Dowfen
Dear god....

I'm either really, really stupid or that still doesn't work.

Eric

Posted: Fri Dec 12, 2003 1:15 am
by microthick
Ok, let's do it plain html first.

<form action="page.php" method="post">
<input type="radio" name="test" value="test">
<input type="submit" value="Submit!">
</form>

And you are selecting the radio button before pressing Submit! right?

Posted: Fri Dec 12, 2003 1:24 am
by Dowfen
Yes, I was pushing the radio button.

I want to make sure I'm clear so I don't catch myself in an embarassing error and waste your time.

With what you've given me (the plain html) this is what I've tried:

------------------test.php----------------------------

Code: Select all

<form action="page.php" method="post"> 
<input type="radio" name="test" value="test"> test</input>
<input type="submit" value="Submit!"> 
</form>
-------------------page.php----------------------------

Code: Select all

<?php 
echo $_POST&#1111;'value']; 
?>
I tried it with and without the closing </input> tag. Neither worked. I also tried 'name' and 'value' in the $POST echo.

Thanks again,

Eric

Posted: Fri Dec 12, 2003 1:49 am
by microthick
Radio buttons don't have a </input>.

Let's try this:

Code: Select all

<form action="page.php" method="post"> 
     <input type="radio" name="mytestradiobutton" value="testvalue"> radio button description
     <input type="submit" value="Submit!"> 
</form>


Then on page.php:

Code: Select all

<?php 
echo $_POST&#1111;"mytestradiobutton"]; // should output testvalue 
?>
If that doesn't work, replace the above with:

Code: Select all

<?php 
echo $HTTP_POST_VARS&#1111;"mytestradiobutton"]; // should output testvalue 
?>