dsomm80482 2017-03-15 19:55
浏览 63

如何将变量从jquery传递给php

I've searched and searched and can't seem to figure it out from existing posts. I have some specific code and I don't know how to manipulate it to get what I need. //======================== Here is the HTML:

        $q_id     = get_question_id($holder_id, $pointer);
        echo "HERE:  ". $q_id; // This line works!  I get an id number here.  
?>
<html>
<head><title>jQuery + JSON + PHP + AJAX</title></head>
<body>
        <div class="container">
        <div class="col-md-6">
          <form id="myForm" class="form-signin" action="react.php" method="post">
          <input type='hidden' name='holder_id'   value='<?php echo $_POST['holder_id']; ?>'/>
          <input type='hidden' name='pointer'   value='<?php echo $_POST['pointer']; ?>'/>
          <input type='hidden' name='q_id'   value='<?php echo $q_id; ?>'/>
        <h3>Question</h3>
        <ul></ul>
        <button id="sub" class="btn btn-lg btn-primary btn-block" type="submit">Create</button>
        </form>
        <h3 id="result"></h3>

//========================

Here is the script.js:

    function reads() {
     $.getJSON("fetch.php", function(data) {
       $("ul").empty();
       $.each(data.result, function(){
        var output = $("").val();
        output = "<li><h4>"+ this['id'] +": "   + this['question']      +"</h4></li>";

        output += "<div class=\"checkbox\">";
        output += "<label><input name=\"a\" type=\"checkbox\">"+ this['a'] +"</label><br/>";
        output += "<label><input name=\"b\" type=\"checkbox\">"+ this['b'] +"</label><br/>";
        output += "<label><input name=\"c\" type=\"checkbox\">"+ this['c'] +"</label><br/>";
        output += "<label><input name=\"d\" type=\"checkbox\">"+ this['d'] +"</label><br/>";
        output += "</div>";

        $("ul").append(output);
       });
 });
}

//========================

Here is the fetch.php:

    <?php
        include_once('db.php');
        $q_id       = $_POST['id'];
        $sql = "SELECT * FROM questions WHERE id = {$q_id} LIMIT 1";

        $res = mysqli_query($connection, $sql);
        $result = array();

        while( $row = mysqli_fetch_array($res) )
            array_push($result, array(  'id'        => $row['id'],
                                        'question'  => $row['question'],
                                        'a'         => $row['a'],
                                        'b'         => $row['b'],
                                        'c'         => $row['c'],
                                        'd'         => $row['d']
                                        ));

        echo json_encode(array("result" => $result));
?>

I'm new to learning this sort of thing and I'm having problems extracting the $_POST['id'] just before the sql statement in the "fetch.php" file. Normally, I can put print_r($_POST) near the top of any php page and see all the vars listed. Under these circumstances, I'm not seeing any vars arrive at the php file. If I put print_r($_POST) on the html, I get values from the previous page, but the fetch.php file doesn't seem to be getting any POST vars.

I imagine the answer to my quandary is probably something very elementary, but I need help and reading through all these posts that are very similar but not quite similar enough to my problem isn't getting me very far. I've actually gotten this far, not knowing much, just by reading posts...so I am trying and I hope there's not a post out there that I just didn't see or learn from before posting this.
Thanks!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥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系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。