Page 1 of 1
Why doesn't this form work (I've broken down)
Posted: Thu Dec 18, 2003 7:12 pm
by Dowfen
Hello,
I posted about this problem a couple weeks ago and have been very busy between then and now with work so I havn't had the opportunity to work on my project very much, so I was hoping someone could tell me why this form doesn't work:
Code: Select all
<form action="deletelinks.php" method="post">
<?
**MYSQL CONNECT STUFF REMOVED**
print("<strong>" . "Please select the links you'd like to delete:" .
"</strong><br></br>");
while ($row = mysql_fetch_array($result)) {
print("<input type=checkbox value="" . $rowї"title"] . "">" . $rowї"title"]
. "</input><br>");
}
?>
<center><input type="submit"></center>
</form>
------------------------------deletelinks.php-------------------------------
Code: Select all
<?php
echo $_POSTї'value'];
?>
I'm just trying to get the form to send and output some data so I can move on. I simply have not been able to figure this out.
Again, I really appreciate everyone's help.
Eric
Posted: Thu Dec 18, 2003 7:21 pm
by qads
you check box is missing name="" part.
Posted: Thu Dec 18, 2003 7:42 pm
by Dowfen
Yeah, I've had it with a name, without a name, etc. it still does not work.
I appreciate the reply though,
Eric
Posted: Thu Dec 18, 2003 8:48 pm
by Weirdan
what the output from the first code section looks like?
Posted: Thu Dec 18, 2003 8:49 pm
by Gen-ik
Try this and see what happens. Dump the code into one php file and run it.
Code: Select all
<?php
if(count($_POST)>0)
{
$inputValue = $_POST["myInput"];
echo "Hmmmm tasty <b>{$inputValue}</b>.<hr />";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<select name="myInput">
<option value="Apples">I like Apples</option>
<option value="Bananas">Monkies like Bananas</option>
</select>
<br />
<input type="submit" value="submit" />
</form>
Posted: Thu Dec 18, 2003 8:55 pm
by Weirdan
did you mean
Code: Select all
<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
?

Posted: Thu Dec 18, 2003 9:01 pm
by Gen-ik
oh yeah (doh!).... changed it.
That's what happens when you try and code at 3 in the morning.
Posted: Fri Dec 19, 2003 12:12 am
by Dowfen
That works and outputs "Hmmm tasty Apples."
I do not understand why mine doesn't. When you view the source of the page it looks just like a normal form. With a "name" and "value" value.
I appreciate all the help guys,
Eric
Posted: Fri Dec 19, 2003 8:26 am
by Gen-ik
Don't forget that a checkbox will only post it's value with a form if it is checked.
If you are using multiple inputs with the same name don't forget to put square brackets after each name, for example..
name="box[]" value="linkOne"
name="box[]" value="linkTwo"
..you will then find that $_POST["box"] is an array and not a single value.
On the page that you submit the form to add var_dump($_POST) and see what it chucks out. You will then be able to see if the values are actually getting to PHP ok.
Posted: Fri Dec 19, 2003 9:42 am
by choppsta
To summarise, your code would look something like this...
Code: Select all
<?php
//**MYSQL CONNECT STUFF REMOVED**
echo '<form action="deletelinks.php" method="POST">';
echo '<strong>Please select the links you''d like to delete:</strong><br>';
while ($row = mysql_fetch_array($result)) {
$row["title"] = htmlspecialchars($row["title"]);
echo '<input type="checkbox" [b]name="links[]"[/b] value="'.$row["title"].'"> '.$row["title"].'<br>';
}
echo '<center><input type="submit"></center>';
echo '</form>';
?>
Notice I've done a htmlspecialchars to the title, otherwise, any title with double quotes in it will break your page...
And deletelinks.php...
Code: Select all
<?php
if (is_array($_POST["links"]) {
foreach ($_POST["links"] as $value) {
echo $value.'<br>';
}
}
else {
echo 'No boxes checked.';
}
?>
Posted: Fri Dec 19, 2003 8:23 pm
by Dowfen
Unexpected '{' on line 3 on the deletelinks.php page.
Everything looks right to me...but then again I know little. <grin>
Eric
Posted: Fri Dec 19, 2003 10:45 pm
by Weirdan
Hmm, tasty unbalanced parenthesis

should be:
Code: Select all
if (is_array($_POST["links"])) {
foreach ($_POST["links"] as $value) {
echo $value.'<br>';
}
}
else {
echo 'No boxes checked.';
}
Posted: Sat Dec 20, 2003 1:01 am
by Dowfen
I'm sorry you had to point that out.

I need to work on this stuff when I'm semi-conscious.
Eric
Posted: Sat Dec 20, 2003 1:09 am
by Dowfen
choppsta wrote:To summarise, your code would look something like this...
Code: Select all
<?php
//**MYSQL CONNECT STUFF REMOVED**
echo '<form action="deletelinks.php" method="POST">';
echo '<strong>Please select the links you''d like to delete:</strong><br>';
while ($row = mysql_fetch_array($result)) {
$row["title"] = htmlspecialchars($row["title"]);
echo '<input type="checkbox" [b]name="links[]"[/b] value="'.$row["title"].'"> '.$row["title"].'<br>';
}
echo '<center><input type="submit"></center>';
echo '</form>';
?>
Notice I've done a htmlspecialchars to the title, otherwise, any title with double quotes in it will break your page...
And deletelinks.php...
Code: Select all
<?php
if (is_array($_POST["links"]) {
foreach ($_POST["links"] as $value) {
echo $value.'<br>';
}
}
else {
echo 'No boxes checked.';
}
?>
I was concerned about that, but hadn't gotten to that point. However, with the special chars things like apostrophes get output wrong as well. I don't suppose there's anyway to correct this (simply) while still keeping the check for "" in?
Posted: Sat Dec 20, 2003 1:42 am
by Dowfen
Amazingly I was able to fix it on my own. I appreciate all the help guys. You helped me get my page working. I'll finally let this thread die.
Eric