Page 1 of 1

cannot make self-referencing script work

Posted: Fri Dec 05, 2003 4:41 pm
by kirilisa
So I am trying to make a simple form that once you fill it out it calls itself (the same script) as the form action. I am running into all sorts of problems testing the existence of members of the _POST array.

I read around and see that in associative arrays you are supposed to use single quotes when indicating an element. However, this never works in my if statements!

e.g. Say I do echo $_POST['op'] and it returns "ds" (which is what the op key is supposed to be equal to given a hidden element in my form named op value ds). Now I know _POST['op'] == ds but when I do

if ($_POST['op'] == "ds") {
do something;
}

it thinks that $_POST['op'] does not == "ds" even though it just echoed it.

See the code below. If anyone can tell me what I am doing wrong, I would really appreciate it. I just can't seem to figure it out.

The result of the code is at http://squelch.mit.edu/allinone_form.php.

--------------Code Begins-----------------------
<html>
<head><title>all-in-one feedback form</title>
</head>
<body>

<?
$form_block = "
<form method=\"POST\" action=\"$_SERVER[PHP_SELF]\">
<p><strong>Your Name Here: </strong><br>
<input type=\"text\" name=\"sender_name\" size=30></p>

<p><strong>Your Email Address Here:</strong><br>
<input type=\"text\" name=\"sender_email\" size=30></p>

<p><strong>Message:</strong><br>
<textarea name=\"message\" cols=30 rows=5 wrap=virtual></textarea></p>

<INPUT type=\"hidden\" name=\"op\" value=\"ds \">

<p><input type=\"submit\" name=\"submit\" value=\"Submit!\"></p>
</form>";


// test 1: I get an error if op IS NOT in single quotes when the variable ISN'T in a quoted echo thing
echo "post op is echoed as equalling "; echo $_POST['op']; echo "<br><br>";

//test 2: I get an error if op IS in single quotes when the variable IS in a quoted echo thing
echo "post op is echoed as equalling $_POST[op]<br><br>";


//In either case, even though the echo statements above prove that op DOES EQUAL "ds", in my if test it says it doesn't equal ds!
if ($_POST['op'] != "ds") {
echo "according to my if statement, post op is NOT ds<br>";
echo "$form_block";
} else if ($_POST['op'] == "ds") {

echo "according to my if statement, post op IS ds<br>";

if ($_POST[sender_name] == "") {
$name_err = "Please enter your name!<br>";
$send = "no";
}

if ($_POST[sender_email] == "") {
$email_err = "Please enter your email!<br>";
$send = "no";
}

if ($_POST[message] == "") {
$message_err = "Please enter your message!<br>";
$send = "no";
}

if ($send !="no") {
$msg = "EMAIL SENT FROM NEW SITE\n";
$msg .= "Sender's Name\t$_POST[sender_name]\n";
$msg .= "Sender's Email:\t$_POST[sender_email]\n";
$msg .= "Sender's Message:\t$_POST[message]\n";
$to = "kirilisa@alum.mit.edu";
$subject = "test email!";
$mailheaders = "From: My Web Site <> \n";
$mailheaders .= "Reply-To: $_POST[sender_email]\n\n";
mail($to, $subject, $msg, $mailheaders);
echo "Mail has been sent!<br>";
} else if ($send == "no") {
echo "$name_err $email_err $message_err<br><br>";
echo "$form_block";
}
}


?>


</body>
</html>
---------------------------Code Ends-----------------------

Posted: Fri Dec 05, 2003 6:15 pm
by Sevengraff

Code: Select all

<html>
<head><title>all-in-one feedback form</title>
</head>
<body>

<?
echo "
<form method="POST" action="$_SERVER['PHP_SELF']">
<p><strong>Your Name Here: </strong><br>
<input type="text" name="sender_name" size=30></p>

<p><strong>Your Email Address Here:</strong><br>
<input type="text" name="sender_email" size=30></p>

<p><strong>Message:</strong><br>
<textarea name="message" cols=30 rows=5 wrap=virtual></textarea></p>

<INPUT type="hidden" name="op" value="ds ">

<p><input type="submit" name="submit" value="Submit!"></p>
</form>";

echo '<pre>';
print_r($_POST);
echo '</pre>';


?>


</body>
</html>
Try looking at the output of that after submitting the form.