Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send single form value to multiple pages in php using jquery

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'];
?>
like image 260
Sowmya S Avatar asked Dec 10 '25 11:12

Sowmya S


1 Answers

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

like image 148
Sarath Damaraju Avatar answered Dec 12 '25 07:12

Sarath Damaraju



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!