How to get data from one php page using ajax and pass it to another php page using ajax

后端 未结 4 525
野性不改
野性不改 2021-01-22 08:45

I am trying to get data from one php page and pass it to another page using Ajax.

JS :

$.ajax({
      url: \"action.php\",
      succes         


        
相关标签:
4条回答
  • 2021-01-22 08:55

    Try to use $.get() method to get/send data :

    $.get("action.php",{}, function(data){
        //data here contain 1
        $.get("data.php", {id: data}, function(id){
             alert(id);
        }
    });
    

    Just echo $test since just the data printed in page will return as responce to the query request.

    action.php :

    <?php    
        $test=1;
        echo $test;
    ?>
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-22 09:00

    For example,

    <a href="#" class="dataClass" data-value="1">Test</a>
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="" src="action.js"></script>
    

    action.js

    $('.dataClass').click(function(){
        var value=$(this).attr('data-value');
        $.ajax({url:"Ajax_SomePage.php?value="+value,cache:false,success:function(result){
            alert("success");
        }});
    }); 
    

    Ajax_SomePage.php

    <?php
    $value = $_GET['value'];
    echo $value;
    ?>
    
    0 讨论(0)
  • 2021-01-22 09:06

    First of all, you need to echo your data in action.php, and second, use data parameter of AJAX request to send data to data.php.

    Here's the reference:

    • jQuery.ajax()

    So the organization of pages should be like this:

    JS :

    $.ajax({
        url: "action.php",
        success: function(data){ 
            $.ajax({
                url: "data.php",
                data: {id: data},
                success: function(data){ 
                    // your code
                    // alert(data);
                }
            });
        }
    });
    

    action.php :

    <?php
        $test = 1;
        echo $test;
    ?>
    

    data.php :

    <?php
        $id = $_GET['id'];
        echo $id;
    ?>
    
    0 讨论(0)
  • 2021-01-22 09:17

    To get data as response in ajax call, you need to echo the result from your php page; action.php page.

    echo $test =  1;
    

    In your provided code

    $.ajax({
        url: "data.php?id=data"
    }       // closing bracket is missing
    

    you are sending the string data as id to data.php page. Instead you have to append the result with the url using + symbol like shown in the below code.

     $.ajax({
       url: "action.php",
       success: function(data){           
        $.ajax({
             url: "data.php?id="+data
        })
      }
    });
    
    0 讨论(0)
提交回复
热议问题