skip to Main Content

I have developed the website using jquery & PHP, I have created a page using HTML, / PHP so what I want is if I click on submit, item ajax should send value to PHP variable and then execute index.php and get that value to another page.

Please help me to resolve the issues.

So far, I have tried the following:

index.php

my Javascript Code

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#btn").click(function() {
        var val = $("#number").val();
        $.ajax ({
             type: "POST",
            url: "ajax.php",
            data: { val : val },            
            dataType:'text',
            success: function( result ) {           
            // window.location.href = 'ajax.php'; 
            // alert(result);           
             }
        });
    });
});
    </script>

my HTML Code

    <form name="numbers" id="numbers">
     <input type="text" name="number" id="number" >
     <button type="button" id="btn">Submit</button>
    </form>

ajax.php

    <?php
    session_start();
 $_SESSION['val'] = $_POST['val'];
print_r($_SESSION);
    ?>

3

Answers


  1. You can store the value in session and redirect the user to other page and get data from session at other pages.

    <?php
    session_start();
    $_SESSION['data'] = $_GET['val'];
    ?>
    

    Your HTML code must be like that

        <form name="numbers" id="numbers">
         <input type="text" name="number" id="number">
         <button type="button" id="btn">Submit</button>
        </form>
    

    For redirect to other page you can use like

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $("#btn").click(function() {
            var val = "Hi";
            $.ajax ({
                url: "ajax.php",
                data: { val : val },
                success: function( result ) {     
                 window.location.href = 'url';            
                 }
            });
        });
    });
    </script>
    
    Login or Signup to reply.
  2. in this if you click button its refresh the page because you using input type=”submit” on button , so just change it to input type=”button”

    <input type="button" id="btn" value="submit">
    

    and if you want to keep input type=”submit” as you already did then just add prevent default to jquery

    $(document).ready(function(){
    $("#btn").click(function(event) {
    event.preventDefault();
        var val = "Hi";
        $.ajax ({
            url: "ajax.php",
            data: { val : val },
            success: function( result ) {                
             }
        });
      });
    });
    
    Login or Signup to reply.
  3. you can use session

    in ajax.php you have to add:

    <?php
    session_start();
    echo $_GET['val'];
    $_SESSION['val'] = $_GET['val'];
    ?>
    

    and use variable $_SESSION[‘val’] to any page

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search