【问题标题】:How can I save a DropDown into a Database with AJAX如何使用 AJAX 将下拉菜单保存到数据库中
【发布时间】:2017-09-05 15:14:18
【问题描述】:

我在这方面非常努力,但我无法理解 AJAX 与 PHP 的结合。

这就是我所拥有的,当用户点击下拉菜单时,我希望它保存到我的数据库中

<select>
  <?php $taskStatus = "SELECT * FROM task_status WHERE used = 1 ORDER BY id ASC ";
   $taskresults = $conn->query($taskStatus) or die(mysqli_error($conn));
   while($taskStatusRow = mysqli_fetch_assoc($taskresults)) {
     echo " <option  value= ". $taskStatusRow['name'] ." >". $taskStatusRow['name'] ." </option>";
   }
  ?>
</select>

这是我要运行的查询:

INSERT INTO snagging (taskstatus, updated_at) 
WHERE ID = 1234 
VALUES taskStatusRow['name'], $now);

【问题讨论】:

  • 嗯,AJAX 涉及到 JavaScript,所以......你的 JavaScript 代码在哪里?
  • 我不知道如何处理 JS..so
  • 然后它看起来像是回到了书籍和教程。它真的不是火箭科学
  • 网上已经有数百个 ajax 示例,甚至可能针对这个确切的用例。您可以尝试一个并适应您的需求。无需在这里重新发明轮子。如果您尝试某事并遇到困难,请在此处发布您的尝试以及遇到的任何错误。

标签: javascript php jquery sql ajax


【解决方案1】:

我会在这里给你一个 AJAX 的整体流程。我尝试提供 cmets 以显示控制流。

<select id="selectOption">  //******* Assign an ID
    <?php $taskStatus = "SELECT * FROM task_status WHERE used = 1 ORDER BY id ASC ";
    $taskresults = $conn->query($taskStatus) or die(mysqli_error($conn));
    while($taskStatusRow = mysqli_fetch_assoc($taskresults)) {

        echo " <option  value= ". $taskStatusRow['name'] ." >". $taskStatusRow['name'] ." </option>";

    }
    ?>
</select>

jQuery + AJAX

$(document).ready(function() {
    $("#selectOption").change(function(){ //** on selecting an option based on ID you assigned
        var optionVal = $("#selectOption option:selected").val(); //** get the selected option's value

        $.ajax({
            type: "POST", //**how data is send
            url: "MYPROCESSPAGE.php", //** where to send the option data so that it can be saved in DB
            data: {optionVal: optionVal }, //** send the selected option's value to above page
            dataType: "json",
            success: function(data){
                //** what should do after value is saved to DB and returned from above URL page.
            }
        });
    }); 
});

在您的MYPROCESSPAGE.php 中,您可以访问通过 AJAX 传递的数据,例如:

<?php

$selectedOptionVal = $_POST['optionVal'];

//DB CONNECTION STEPS
.
.
.
// You are trying to "UPDATE" a table data based on some ID and not inserting. Included both operations

// If you are INSERTING A new table entry, use below code.
//INSERT INTO snagging (taskstatus, updated_at) VALUES ('$selectedOptionVal', 'Now()');

// If you are UPDATING an existing table entry, use below code.
//UPDATE snagging SET taskstatus = '$selectedOptionVal', updated_at = 'Now()' WHERE ID = 1234;

?>

希望对你有帮助。

【讨论】:

  • 很高兴我能帮上忙。
  • @Gem 这是因为你不能在 ajax.php 中使用$_POST['price'],因为你没有传递任何&lt;input type="" name="price"&gt; 字段。您不能通过将其命名为“价格”来访问 div 内的价格。
  • 您可以将 price 作为隐藏字段传递为 &lt;input type="hidden" name="price"&gt; 并更改以下内容:&lt;b&gt;&lt;div id="result" name="price"&gt;&lt;/div&gt;&lt;/b&gt;&lt;b&gt;&lt;div id="result"&gt;&lt;/div&gt;&lt;/b&gt;&lt;input type="hidden" name="price" id="price"&gt; 并在下拉菜单中将其更改成功 $('#result').html(data);$('#result').html(data);$('#price').val(data);
  • $price 变量中设置成本,在$shipping_charge 中设置运费,而不是echo "2500";,并在必要时回显这两个变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 2019-10-20
  • 1970-01-01
  • 2016-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多