views:

44

answers:

4

Why doesn't this validate with the W3C validator:

3 variables from form.html going into form.php:

  <?php

   $stuff1 = $_POST["stuff1"];//catch variables
   $stuff2 = $_POST["stuff2"]; 
   $stuff3 = $_POST["stuff3"]; 
   $myStuff[0] = $stuff1;//put into array
   $myStuff[1] = $stuff2;
   $myStuff[2] = $stuff3;
  ?>   
+7  A: 

Why doesn't this validate with the W3C validator:

You may be misunderstanding something here. PHP code is generated on the server side, and outputs HTML (or not). Your abovementioned script will not pass any HTML validator, because to the validator, it will be empty. PHP and the W3C validator have nothing to do with each other.

If you are getting a PHP error message, please post it.

Pekka
+1 - I think understanding that difference is probably more useful to the OP than dealing with what is wrong with his code; and for use of the word "abovementioned" (http://dictionary.reference.com/browse/abovementioned, for you doubters).
Dominic Rodger
A: 

It seems to be fine but try this too:

 $stuff1 = $_POST["stuff1"];
 $stuff2 = $_POST["stuff2"]; 
 $stuff3 = $_POST["stuff3"]; 

 $myarray = array();
 $myarray[] = $stuff1;
 $myarray[] = $stuff2;
 $myarray[] = $stuff3;

 print_r($myarray);

Also make sure that you put your fields inside a form eg form tag.

Note that php code is not something to be validated by the W3C validator, it is server-side generated code.

Sarfraz
Yes your all correct with the difference some mistakes one make..., thanks!
Chris_45
A: 

Try declaring your array:

$stuff1 = $_POST["stuff1"];//catch variables
$stuff2 = $_POST["stuff2"]; 
$stuff3 = $_POST["stuff3"]; 
$myStuff = array();
$myStuff[0] = $stuff1;//put into array
$myStuff[1] = $stuff2;
$myStuff[2] = $stuff3;

and note Pekka's answer about the difference between PHP (server-side) and HTML (client-side).

Dominic Rodger
A: 

Try var_dump($_POST)

or a bit of validation

if (isset() && !empty($_POST["stuff1")) {
    $myStuff[0] = $_POST["stuff1"];
} else {
    echo '$_POST["stuff1"] error';
}

Check everything is being sent across correctly...

Simon