dsz90288 2016-07-26 11:33
浏览 24

PHP Array AJAX Request共享密钥

I have the following SQL query which is performed via an AJAX server request on form submission. It checks a form POST variable $product for any associated products in the database limiting the results to 2. I need to be able to uniquely identify each result from the query using PHP and store them in variables as $product1 and $product2, then return these values to the form without page refresh. Why are the PHP SQL query results not automatically assigned a unique key in the associative array? (see image below)

$product = $_POST['product'];

$sql_query3 = "Select tbl_mixed_case.related_product
FROM tbl_mixed_case JOIN tbl_product_info 
ON tbl_product_info.id = tbl_mixed_case.prod_code_id 
AND tbl_product_info.product + ' ' = '$product' LIMIT 2" ;

$result3 = mysqli_query($dbconfig, $sql_query3);

while ($products = mysqli_fetch_array($result3, MYSQL_ASSOC)) {
print_r(array_values($products));
}

enter image description here

  • 写回答

1条回答 默认 最新

  • dongzhuoxie1244 2016-07-26 11:41
    关注

    Try the following:

    $product = $_POST['product'];
    
    $sql_query3 = "Select tbl_mixed_case.related_product
    FROM tbl_mixed_case JOIN tbl_product_info 
    ON tbl_product_info.id = tbl_mixed_case.prod_code_id 
    AND tbl_product_info.product + ' ' = '$product' LIMIT 2" ;
    
    $result3 = mysqli_query($dbconfig, $sql_query3);
    var $dispProd
    while ($products = mysqli_fetch_array($result3, MYSQL_ASSOC)) {
    $dispProd[] = $products;
    }
    header('Content-Type: application/json');//add the proper header
    echo json_encode(['products'=>$dispProd]);//convert to json
    

    in your ajax success function you do the following:

    success:function(data) {
      $('#product1').val(data.products[0]);//add the first value to a input with the id of product1
      $('#product2').val(data.products[1]);
    }
    

    I have included the ajax request and form input below:

    function submitdata() {
    var product  = document.getElementById("product").value;
    // Returns successful data submission of associated products
    var dataString = 'product=' + product;
            // AJAX code to submit form.
                    $.ajax({
                    type: "POST",
                    url: "product.php",
                    data: 'application/json; charset=utf-8',
                    cache: false,
                    success: function(data) {
                    alert(data);
                     $('#product1').val(data.products[0]);//add the first value to a input with the id of product1
                     $('#product2').val(data.products[1]);
                    }
                    });
    
    }
    
    
    <input type="text" value="" placeholder="" class="" id="product1" name="product1" tabindex="-1"/>
    <input type="text" value="" placeholder="" class="" id="product2" name="product2" tabindex="-1"/>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)