Jump to content

Html 5 question about forms and php check


fuzzylr

Recommended Posts

Greetings, I am trying to write a small page where the user selects how many users. I want to use a drop down list. I believe I have everything in place. However, I can not get the submit information into my php script. Any thoughts? HTML / PHP code

<?php$num=$_POST["num"];$errorif ($num < 1) $error="Invalid option";if ($num == 1) header('Location: driven1.html');if ($num == 2) header('Location: driven2.html');if ($num == 3) header('Location: driven3.html');else header('Location: driven4.html');?><!DOCTYPE HTML><html><head><link href="driven1.css" rel="stylesheet" type="text/css"/><title>Driven support request</title></head><body><?php echo $error ?><div class="body"><header class="header"><!--Accelera logo--><img src="AcceleraLogo.jpg" alt="Accelera Logo" /></header><div class="div1"><form method="get">  <fieldset class="user1">   <label>How many users?</label> <br />   <select name="num">	<option value="1">1</option>	<option value="2">2</option>	<option value="3">3</option>	<option value="4+">4+</option>   </select> <br />   <input type="submit" value="Submit" />  </fieldset></form></div></div></body></html>

Link to comment
Share on other sites

It could be because you don't have a semicolon after $error. I would probably just consolidate it to this:

<?php$num=$_POST["num"];$error = '';if ($num < 1) {  $error = 'invalid option';} else {  header('Location: driven' . $num . '.html');};?>

You might also want to specify the action too. Since you are submitting the form to itself, you could do this:

<form method='get' action='<?php echo $_SERVER['SCRIPT_NAME'] ?>'>

http://php.net/manua...bles.server.php

Link to comment
Share on other sites

It could be because you don't have a semicolon after $error. You might also want to specify the action too. Since you are submitting the form to itself, you could do this:
<form method='get' action='<?php echo $_SERVER['SCRIPT_NAME'] ?>'>

http://php.net/manua...bles.server.php

I've not touched PHP in a while. I was never really all that good at it. So what your saying is convert my php script to a function and just add that to in place of the script name? I read through the php page you posted. I believe in order for this to work I'd have to convert to a function? Would you mind expanding on this? Maybe I'm over complicating the subject.
Link to comment
Share on other sites

i think you must be, because i don't know where you are getting functions from. $_SERVER is a global array just like POST or GET, but it already has members. Using SCRIPT_NAME was just a fail safe I'm suggesting in case not including an action attribute was causing your problem, but it's most likely that you didn't have a semi-colon after $error.

Link to comment
Share on other sites

i think you must be, because i don't know where you are getting functions from. $_SERVER is a global array just like POST or GET, but it already has members. Using SCRIPT_NAME was just a fail safe I'm suggesting in case not including an action attribute was causing your problem, but it's most likely that you didn't have a semi-colon after $error.
Actually, it still wasn't working. So what I did was turn my script into a function. I then added that function to the action piece. I need to create the 4 pages. However, I believe it is working. Function
function number($num){if ($num < 1){  $error="Invalid option";}  else{  header('Location: driven' + $num + '.html');}}

Update to Form

<form method="get" action='<?php echo $_SERVER['number($num)']>

Link to comment
Share on other sites

Actually, it still wasn't working. So what I did was turn my script into a function. I then added that function to the action piece. I need to create the 4 pages. However, I believe it is working. Function
function number($num){if ($num < 1){  $error="Invalid option";}  else{  header('Location: driven' + $num + '.html');}}

Update to Form

<form method="get" action='<?php echo $_SERVER['number($num)']>

Well maybe not. It doesn't seem to be submitting the number.
Link to comment
Share on other sites

Your form uses a get method and you are trying to get the data using the post global variable, use

$num = $_GET['num'];

instead of

$num = $_POST['num'];

or change your form method to POST

Link to comment
Share on other sites

Your form uses a get method and you are trying to get the data using the post global variable, use
$num = $_GET['num'];

instead of

$num = $_POST['num'];

or change your form method to POST

Believe it or not it's not posting. It's not doing anything with the information. I've validated both html and php. I created a much simpler version of the code. It's still not working. I'm going to post up in the php forums.
Link to comment
Share on other sites

Is there any case where the page doesn't redirect? It looks like the HTML is never actually printed.
It's not printing at all. It's very wierd. Here is a very simple version of what I am trying to accomplish. It won't even do what it's suppose to. index.html
<!DOCTYPE HTML><html><head><title>Test PHP page</title></head><body><form method="post" action=""><label>Name:</label><input type="text" name="name" required><label>Age:<lable><input type="number" name="age" required><input type="submit" value="Submit" name="submit"></form></body></html><?php echo $_REQUEST['name'] ?><br /> <?php echo $REQUST['age'] ?>

Link to comment
Share on other sites

so you're file isn't called index.php? I think it should. also, this is wrong, there's no underscore and no E

<?php echo $REQUST['age'] ?>

anyway, when I made it a php page, I got it to work.

<!DOCTYPE HTML><html>  <head><title>Test PHP page</title></head>  <body>	<form method="post" action="">	  <label>Name:</label>	  <input type="text" name="name" required>	  <label>Age:</label>	  <input type="number" name="age" required>	  <input type="submit" value="Submit" name="submit">	</form>  </body></html> <?php echo $_REQUEST['name']; ?><br /><?php echo $_REQUEST['age']; ?>

Link to comment
Share on other sites

And again, the lack of semicolons. Every statement in PHP must end in a semicolon. And just to emphasize what scientist said, your file should be named index.php not index.html, unless your server is specifically set up to parse html files as php.

Link to comment
Share on other sites

so you're file isn't called index.php? I think it should. also, this is wrong, there's no underscore and no E
<?php echo $REQUST['age'] ?>

anyway, when I made it a php page, I got it to work.

<!DOCTYPE HTML><html>  <head><title>Test PHP page</title></head>  <body>	<form method="post" action="">	  <label>Name:</label>	  <input type="text" name="name" required>	  <label>Age:</label>	  <input type="number" name="age" required>	  <input type="submit" value="Submit" name="submit">	</form>  </body></html> <?php echo $_REQUEST['name']; ?><br /><?php echo $_REQUEST['age']; ?>

Setting it index.php does not change the status. It's not displaying any information
<?php$Name = $_POST['name'];$Age = $_POST['age'];?><!DOCTYPE HTML><html><head><title>Test PHP page</title></head><body><form method="post" action="index.php"><label>Name:</label><input type="text" name="name" required><label>Age:<lable><input type="number" name="age" required><input type="submit" value="Submit" name="submit"></form></body></html><?php// Testing to see if the submit button is workingif (isset($_POST['submit'])){echo "You have hit submit";echo $Name;echo $Age;}?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...