从表单外部的PHP页面检索值,以查询MSSQL


Retrieving values from a PHP page, outside a form, to query MSSQL?

我知道问题标题可能令人困惑,但以下是我试图实现的目标:

我在一张桌子里放了一个UL,里面摆满了很多东西。列表的其中一个单元格包含一个复选框。这些复选框都在一个数组"itemsDone[]"中命名。我在侧菜单中有一个按钮(在表单之外)。我希望这个按钮向我的服务器发送一个查询,以更新选中的每个复选框的值。。。有可能吗?我已经在谷歌上搜索了大约一个小时,我可能找错了关键词。。。如果有任何帮助,我将不胜感激。

谢谢你们,祝你们今天愉快!

编辑:PHP页面中根本没有表单。

编辑2:这是我现在拥有的代码:

<script type="text/javascript">
$(document).ready(function(){
    $("#saveCheckboxes").click(function(){
        var check = Array();
        var counter= 0;
        $('.itemsDone').each(function(){
            if(this.checked)
            {
                check[counter] = this.value;
                counter+=1;
                console.log(check[counter]);
            }
        })
        $.post('setCommandes.php',{'check[]':check}, function(data) {
            alert(data);
        });      
       return false;
    });
});
</script>

以下是我的setCommandes.php内容:

<?php
    include('includes/connexion.php');
    $data = $_POST['check[]'];
    $db_select=mssql_select_db("Test_CommandesWeb");
    foreach ($data as $checks)
    {
        $sql = "UPDATE Commandes SET CommandeFaite='1' WHERE CommandeId='$checks'";
        mssql_query($sql,$connexion);
    }
?>

目前,我的foreach语句出现错误(提供了无效参数)。

您应该能够使用jQuery.post()来完成此操作

我不确定你的页面是什么样子的,但我想代码应该是这样的:


//确保使用正确的选择器

//so, do the following when .class is clicked
$(".class").click(function(){
    //select the value 
    $('select.foo').val();
    //post the data
    $.post('ajax/test.html', function(data) {
        //replace the .result div with the
        //response from the server
        $('.result').html(data);
    });      
});

您可以使用jQuery来实现这一点,比如:

$('#myButton').click(function(){
    // loop through all checkboxes and check if they're checked
    $('input[type=checkbox]').each(function () {
       if (this.checked) {
           // do something with the checkbox if it's checked, like make ajax call
    });
});

或者,你可以用PHP来完成。只需将提交按钮放在表单中,提交时,访问数组itemsDone[]并查看是否已选中。

按钮必须在表单中才能提交。您的表单应该包含您想要的所有输入以及按钮。

<form method="POST" action="/mark_items_done.php">
  <ul>
    <li>
      <input type="checkbox" name="itemsDone[]" value="1" />
    </li>
    <li>
      <input type="checkbox" name="itemsDone[]" value="2" />
    </li>
    <li>
      <input type="checkbox" name="itemsDone[]" value="3" />
    </li>
    <li>
      <input type="checkbox" name="itemsDone[]" value="4" />
    </li>
    <li>
      <input type="checkbox" name="itemsDone[]" value="5" />
    </li>
    <li>
      <input type="checkbox" name="itemsDone[]" value="6" />
    </li>
  </ul>
  <input type="submit" value="Send Items" />
</form>

在您的PHP页面上,您可以使用获得itemsDone

<?php
$items_done = isset($_POST['itemsDone']) ? $_POST['itemsDone'] : null;
if( $items_done ) {
  // do something with the items
}

如果您不想在表单中移动按钮,可以使用jQuery执行类似的操作。

$('#submit_button').click(function() {
  $('#my_form').submit();
});

这个解决方案将重新加载页面,但如果需要的话,实现AJAX应该不会太困难。

考虑下一步:

1) 您可以使用javascript通过任何操作提交任何表单。在这种情况下,您的表单必须包含属性name或id:

表单本身:

<form name="formName" id="formId" method="POST" action="anyformhandler.php">
<input type="checkbox" value="opt1" name="itemsDone[]">
<input type="checkbox" value "opt2" name="itemsDone[]">
</form>

提交人外部表格(例如简单文本):

<a href="#" onClick="document.formName.submit()">Submit the form!</a>

<a href="#" onClick="document.forms['formId'].submit()">Submit the form!</a>

2) 在处理表单的php文件中获取数据:

  if ((isset($_POST["itemsDone"])) and (is_array($_POST["itemsDone"]))){
     // just do needed stuff.
    } 

3) 要在使用数据库时更新每个复选框的值,您应该与数据库交互。查询到数据库->存储数据->加载复选框的值。

如果你愿意,可以提问。希望这能有所帮助;)