weixin_33726318 2014-04-10 15:52 采纳率: 0%
浏览 25

jQuery,Ajax请求,PHP

Wondering how I can get if the value of a checkbox if it's checked then I want it be stored in a PHP session if possible.

Not sure this can be done. Hence the question on here!

Here's a small/important part of the code I have.

      <div class="accessories-right">
         <h3><?php echo $row['title'] ;?></h3>
         <p><?php echo $row['description']; ?></p>
         <div style="width: 700px; float: left; min-height: 20px;">
             <div id="ck-button">
                 <label>
                     <input type="checkbox" value="1" style="visibility: hidden;" name="checked" class="box" id="box"><span>SELECT</span>
                 </label>
             </div>
             <div class="accessories-text"><?php echo $row['price']; ?></div>
         </div>
     </div>
        <div style="margin: 20px 0px 40px 0px; padding:0; clear: both;"></div>


    <div style="float: left; margin: 3px 0px 0px 760px;">
        <button class="green-btn" id="more">Continue &raquo;</button>
    </div>

The button has an ID and I have tried something like stated below: But I need it to be on the same page and store in a session to use later.

 $(".success").hide();
        $(".error").hide();

        $("#more").click(function() {
            var checked = $("#box").val();
            var dataString = 'checked='+ checked;

            if(checked == '')
            {
               $(".success").hide();
               $(".error").show();
            }
            else
            {
                $.ajax({
                    type: "POST",
                    url: "accessories_post.php",
                    data: checked,
                    success: function() {
                        $(".success").show();
                        $(".error").hide();
                    }
                });
            }
            return false;
        });

But this isn't really doing what I want it to do.


Sorry I also forgot to add that I'm wanting to like select multiple items then add them to a mail() function to email this info off to the client.

enter image description here

So i'd like to select either or of these items then have it sent to the client via email

  • 写回答

2条回答 默认 最新

  • weixin_33674437 2014-04-10 15:58
    关注

    You should restructure your ajax() call as follows:

    $.ajax({
        type: "POST",
        url: "accessories_post.php",
        data: {checked: checked},    // Take note of this line.
        success: function() {
            $(".success").show();
            $(".error").hide();
        }
    });
    

    This changes the data parameter to an object, whose checked property will be either true or false (the value of the variable checked. The reason it's done this way is because if you just set data to true or false, there's no effect. POST data needs to be sent as a set of key / value pairs.

    On the PHP side, you can then evaluate whether or not the checkbox is checked with this code:

    $isChecked = (isset($_POST["checked"])) ? (bool)$_POST["checked"] : false;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog