【问题标题】:Retrieving values from a PHP page, outside a form, to query MSSQL?从表单外部的 PHP 页面中检索值以查询 MSSQL?
【发布时间】:2012-07-23 19:02:14
【问题描述】:

我知道问题标题可能令人困惑,但这是我想要实现的目标:

我的 UL 表格中有很多物品。列表的一个单元格包含一个复选框。这些复选框都在数组“itemsDone []”中命名我在侧面菜单中有一个按钮(在表单之外)。我希望这个按钮向我的服务器发送一个查询,以更新每个选中的复选框的值......这可能吗?我已经在谷歌上搜索了大约一个小时,我可能正在寻找错误的关键字...如果能提供任何帮助,我将不胜感激。

谢谢你们,祝你们有美好的一天!

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

Edit2:这是我现在拥有的代码:

<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 语句出现错误(提供的参数无效)。

【问题讨论】:

  • 更改 $data = $_POST['check[]'];到 $data = $_POST['check'];
  • 谢谢,我知道了。我还必须更改 $('.itemsDone').each(function() 为 $('[name=itemsDone\ [\ ]]').each(function() 因为我使用的是 'name' 属性(叹息)。再次感谢!

标签: php sql-server ajax forms


【解决方案1】:

你应该可以使用jQuery.post()来做到这一点

我不确定您的页面是什么样的,但我猜代码应该是这样的:


//确保使用正确的selector

//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);

    });      

});

【讨论】:

    【解决方案2】:

    你可以用 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[] 并查看它是否被选中。

    【讨论】:

      【解决方案3】:

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

      <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 应该不会太难。

      【讨论】:

        【解决方案4】:

        考虑接下来的步骤:

        1) 您可以使用 javascript 通过任何操作提交任何表单。 在这种情况下,您的表单必须包含属性名称或 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) 要在使用数据库时更新每个复选框的值,您应该与数据库进行交互。查询数据库 -> 存储数据 -> 加载复选框的值。

        如果您愿意,可以提出问题。希望这会有所帮助;)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-04
          • 2020-06-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多