I got a question to a JavaScript code. I got an HTML form:
<form action="getValue.php" method="post" id="myform">
<input type="text" id="mybox" name="mybox" />
<input type="submit" id="submit" name="submit" value="Submit" />
</form>
I want to alert the value by clicking submit button. For this I use this code:
$(document).ready(function(){
$('#myform').submit(function(event){
alert(document.getElementById('mybox').value);
});
});
If I type in for example "123" it alerts "123". Okay, this works. Now I want to run this JavaScript code in PHP echo
. For this I tried this:
if(isset($_POST['submit'])){
echo "<script>alert(document.getElementById('mybox').value);</script>";
}
If I now type in "123" it returns " ". So nothing returns. Can someone explain me why this is or how to fix this?
Cheers
EDIT: It is all in the same file (getValue.php)
Your text box will not be populated when the page has been reloaded.
Try echo'ing the actual post value.
if(isset($_POST['submit'])){
echo "<script>alert('" . $_POST['mybox'] . "');</script>";
}
Edit:
As you pointed out that you were new to PHP & JavaScript I thought I'd mention that you seem to be using half jQuery and half non-jQuery...
For example instead of using
document.getElementById('mybox').value
You can use
$('#mybox').val();
echo '<script type="text/javascript">alert("Data has been submitted to ' . $_POST['mybox'] . '");</script>';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.