Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
share|improve this question
 
possible duplicate of 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? –  DanFromGermany Oct 17 '13 at 13:37
show 2 more comments

1 Answer

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'] : '';

?>
share|improve this answer
 
@DanFromGermany thanks, post updated. –  Latheesan Kanes Oct 17 '13 at 13:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.