Form submit in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peleve
Forum Newbie
Posts: 3
Joined: Fri Apr 01, 2011 12:10 pm

Form submit in PHP

Post by peleve »

I want to create a very simple database driven survey whose URL is accessed by a mobi Tag via a smart phone, filled out by users, and the results stored in the database. The survey questions can change with time. So the database has three tables: one containing the survey description and questions, one containing the options to the questions (multiple choice with buttons), and one containing the answers.
I create the form with questions and options with buttons, drawing the data from the database. The problem is that I cannot get the "submit" to work. No matter what I try, I get:
The requested URL /Espresso-Sites/mobitags/$_SERVER[ was not found on this server.
You can see it at work at this URL: http://www.ridgeviewvalley.com/testing/test.php
With or without the CSS file the result is the same. Below is the code.
I've been fighting this for two days. Can anyone help me?
Thanks

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<title>"Testing"</title>
</head>
<body>

<?php
// Create the form
	$question = "How good is this?";
	$option = array();
	$option[0] = "This is good";
	$option[1] = "This is better";
	$option[2] = "This is best";
	$number_options = 3;

	$res = "\$_SERVER"."['PHP_SELF']";
	echo($res . "</br>");
	echo($_SERVER['PHP_SELF']);
	echo("<form class='form' method='post' action='$res'>"); 
	echo('<div class="questionblock">');
	echo("<h3>".$question."</h3>");
/*		echo("<p>".$number_options."</p>");*/
	echo('<div class="optionblock">');
	$j = 0;
	while ($j < $number_options)
	{
		echo('<input class="optional" type="radio" name="option" value="option"/> <h5>' . $option[$j] . '</h5>');
		$j++;
	}
		echo('<input class="submit" type="submit" name="Submit" value="Submit">');

	echo("</div>");
	echo("</div>");
	echo("</form>");
		
	echo($_POST['option']."</br>");

?>
</body>
Kurby
Forum Commoner
Posts: 63
Joined: Tue Feb 23, 2010 10:51 am

Re: Form submit in PHP

Post by Kurby »

Check your form action. I believe you want action=$_SERVER[PHP_SELF].
peleve
Forum Newbie
Posts: 3
Joined: Fri Apr 01, 2011 12:10 pm

Re: Form submit in PHP

Post by peleve »

PHP_SELF needs single quotes around it. With that you get the path and page string. Without the single quotes it just echos the PHP_SELF.

Thanks for lookig at it.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Form submit in PHP

Post by McInfo »

Because the dollar sign ($) is preceded by a backslash (\), $_SERVER is not a variable--it is a literal string containing a dollar sign.

Code: Select all

$res = "\$_SERVER"."['PHP_SELF']";
After the assignment statement, the $res variable holds the following string (not whatever is in the variable $_SERVER['PHP_SELF']).

Code: Select all

$_SERVER['PHP_SELF']
Notice that the $res string contains single quotes ('). The HTML string that the $res string is inserted into also contains single quotes.

Code: Select all

echo("<form class='form' method='post' action='$res'>");
Below is the HTML string that the browser receives. Notice that the first single quote in the $res string breaks the HTML and limits the value of the action attribute to "$_SERVER[".

Code: Select all

<form class='form' method='post' action='$_SERVER['PHP_SELF']'>
When you submit the form, the server looks for a file named "$_SERVER[", but is unable to find it, so it gives you a "not found" error instead.

Use $_SERVER['SCRIPT_NAME']. $_SERVER['PHP_SELF'] is sometimes a vector for cross-site scripting because it can contain the query string on some servers. However, read this.
peleve
Forum Newbie
Posts: 3
Joined: Fri Apr 01, 2011 12:10 pm

Re: Form submit in PHP

Post by peleve »

I see the issue with the single quote but I thought that PHP would interpret the string with the $ sign on a new line as a variable. I guess once defined as a string it stays defined that way. I had the problem with the quote when I constructed the attribute for "action" in-line (I needed three types of quotes) and I thought I had solved that by creating the variable $res. But, I didn't think it through like you did. Thanks.

Just before reading your post I got it to work by breaking everything into minimal php and maximal straight html. That got rid the the problem, the quote and the string.

I got the version that you looked at to work by modifying to:
$res = $_SERVER['PHP_SELF']; and
echo('<form class="form" method="post" action="'.$res.'">');

Thanks again.
Post Reply