weixin_33733810 2018-01-08 11:08 采纳率: 0%
浏览 8

将Ajax转换为PHP再转换为Ajax

This might be a bit vague, but I'll do my best to summarize.

What I'm trying to do is get the value of a select element on change, store that result into a variable, then send it via Ajax to a php file called product_final.php. At which point the PHP script will run through the database and pull the database results based on that variable and submit it back to the form so that it can be changed dynamically without the page being reloaded.

Again, a bit vague, but any help will be greatly appreciated. (Here's the code with a simple echo statement in the PHP file where I'm stuck at just trying to echo out the variable sent by Ajax.

jQuery("#field_6_17 select").on('change', function() {

  var productValue = jQuery(this).val();

    $.ajax({
        method: "POST",
        url: "../product_final.php",
        data: {productValue: productValue},
        cache: false,
        success: function(productFinal) {

        console.log("Sample of Product:", productValue);
    }
    });
    });

Here's the PHP trying to echo out the variable (The product_final.php file is blank and does not result in anything currently)

$finalProduct = $_GET['productValue'];
  echo $finalProduct;
  • 写回答

1条回答 默认 最新

  • weixin_33737774 2018-01-08 11:10
    关注

    You should get the value using $_POST method. Updates code as below.

     $finalProduct = $_POST['productValue'];
     echo $finalProduct;
    
    评论
编辑
预览

报告相同问题?