-3

I was trying to make a contact form, but I have some problems :p I have read that the only way to pass a javascript variable to php is via ajax. I have tried some advices from stackoverflow, but nothing worked. So this is my code and I want your opinion. How will I pass tha Javascript variable (jTemp2) to php?

sample.html

<!DOCTYPE html>
<html>
<head>

<title> Hello World </title>

</head>
<body>
<?php

// define variables and set to empty values
$fNameErr =  "";
$fName = "";
$temp = 0; //flag for mail
$temp2=0; //flag for button


if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["fName"])) {
        $fNameErr = "Required";
        $temp=1;
        //echo $temp;
    } else {
        $fName = test_input($_POST["fName"]);
        // check if fName only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/", $fName)) {
            $fNameErr = "Invalid"; 
            $temp=1;
        }
    }
}    

function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}


?>
<script>
    var jTemp2 = "<?php echo $temp2; ?>";
</script>


<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"  id="form1" onsubmit="myFunction(jTemp2)">
<fieldset class="fiel">
<legend><b><span class="words">Contact</span></b></legend>
<table border="0">
<tr><td><b><span class="words"> Name: </span></b></td><td><input type="text" size="30" name="fName" id="fName" value="<?php echo $fName;?>"><span class="words"> *</span></td><td><span class="error"> <?php echo $fNameErr;?></span></td></tr>

<tr><td></td><td id="but"><input type="submit" value="Υποβολή" ></td></tr>
<tr><span class="words">* Required</span></tr>
<tr><td><span ><?php echo $temp; echo $temp2;  ?></span></td></tr>
</table>
</fieldset>
</form>


    <script>
    function myFunction(jTemp2)
    {
        jTemp2++;
    }
    </script>

    </body>
    </html>
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
Kotsarikos
  • 25
  • 4
  • possible duplicate of [How to pass JavaScript variables to PHP?](http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php) – DontVoteMeDown Oct 17 '13 at 13:16
  • You could also do the `++` in php on your `$temp2` variable like this: `$temp2++;` – Joran Den Houting Oct 17 '13 at 13:17
  • 2
    your file extension says `sample.html` though you have php code in it..woww.. – Joke_Sense10 Oct 17 '13 at 13:20
  • Can you give further details about what you're trying to do? – Sahil Sakhuja Oct 17 '13 at 13:33
  • @Joke_Sense10 You know the PHP interpreter doesn't care what filename you feed it? – Daniel W. Oct 17 '13 at 13:37
  • @DanFromGermany You can't run PHP in .html files because the server does not recognize that as a valid PHP extension unless you tell it to. You can do so by adding specific code in .htaccess file on your root web directory, which will tell Apache to process files with a .htm or .html file extension as PHP files. – Joke_Sense10 Oct 17 '13 at 13:42
  • @Joke_Sense10 Apache is not the only webserver. PHP itself doesn't care about file extensions. – Daniel W. Oct 17 '13 at 13:45

1 Answers1

1

Add this inside your <form> ... </form>

<script type="text/javascript">
     document.write('<input type="hidden" name="jTemp2" value="'+ jTemp2  +'">');
</script>

Now, when the form is posted to your PHP script, the variable's value will be inside:

<?php

$jTemp2 = isset($_POST['jTemp2']) ? $_POST['jTemp2'] : '';

?>
Latheesan
  • 23,247
  • 32
  • 107
  • 201