I am trying to pass a value from javascript to php using POST method but it is not working .Here is the code:
<head>
<script type="text/javascript">
function Email()
{
var e=prompt("Your Email","");
if(e==null||e=="")
{
alert("You need to enter an email..Try again");
Email();
}
return e;
}
function Code()
{
var f=prompt("Activation code","");
if(f==null||f=="")
{
alert("You need to enter the code..Try again");
Code();
}
return f;
}
</script>
</head>
<body>
<form method="post">
<input type="hidden" id="Email" name="Email" />
<input type="hidden" id="Code" name="Code" />
</form>
<script>
var email=Email();
var code=Code();
document.getElementByID("Email").value=email;
document.getElementByID("Code").value=code;
</script>
<?php
$email=$_POST["Email"];
$code=$_POST["Code"];
echo $email.$code;
?>
</body>
I get these errors :
- Notice: Undefined index: Email
- Notice: Undefined index: Code
Anybody please help me out…
Okey if you want to print those values you need to create a form form them, a proper one. If you just want to submit form no need for JS there because if you just want to submit some values there is no need for form, you want to use jQuery Post for that.
Fiddle: here
Edit
Then this is what you want. (Note that this code is using jQuery library)