In PHP I am Working on when form has been submitted the values should send to different files Using jquery. example if demo.php form has been submitted data should sent to both process1.php as well as process2.php simultaneously but Its not showing any error and not able to get output. please help me through this code I have tried so far
Demo.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>main form</title>
</head>
<body>
<form action="multi.php" method="post">
Name:<input id ='myname'>
<input type="button" id="btn" value="send to all">
</form>
<script
src="https://code.jquery.com/jquery-1.12.4.min.js"
integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
crossorigin="anonymous"></script>
<script type="text/javascript">
$(function(){
$('#btn').click(function(){
var username = $('#myname').val();
/*first post*/
$.post('multi1.php',{name:username},function(result){
$('#display').append("<br>"+result);
});
$.post('multi2.php',{name:username},function(result){
$('#display').append("<br>"+result);
});
});
});
</script>
</body>
multi1.php
<?php
echo "this is process1";
echo"and you have posted:".$_POST['name'];
?>
multi2.php
<?php
echo "this is process1";
echo"and you have posted:".$_POST['name'];
?>
Check the file names you are posting to and I assume that you are trying to access the posted variable from other php page without storing it.
In general, if you make an HTTP request (in your case, it is POST), the browser will send that value to the other page and will forget it and when you try to access the value from other pages (like, process1.php) it will return an empty string as it didn't store the variable.
So, a solution is to store the value in localstorage using javaScript and try accessing the variable from the other page.
here is the updated code
$(function(){
$('#btn').click(function(){
var $username = $('#myname').val();
window.localStorage.setItem('username', $username);
});
});
and get the stored value with
var $userName = window.localStorage.getItem('username')
you can know about it here and here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With