使用PHP Ajax从MySQL数据库中的On / Off按钮存储值

时间:2013-11-27 11:54:01

标签: php jquery mysql ajax

我找到了一个单身ID的教程。 enter link description here

我根据我的知识修改了多个id,我无法使用ajax获取id。我对ajax并不擅长。我附上了两个代码。任何人都可以告诉我如何修复多个id

我还附上了一个屏幕截图screenshot

的index.php

    <?php
$query=mysql_connect("localhost","pma","pmapass");
mysql_select_db("testdb",$query);
include("connection1.php");
$result = mysql_query("SELECT * FROM mytable ORDER BY id");
?>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>

 <div>
              <table id="datatables" class="display">
                <thead>
                  <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Text</th>
                  </tr>
                </thead>
               <tbody>
                  <?php
                    while ($row = mysql_fetch_array($result)) {
                        ?>
                  <tr>
                    <td><?php echo $row["id"]; ?></td>
                    <td><?php echo $row["name"]; ?></td>
                    <td><?php echo $row["text"]; ?></td>
                    <td><script type="text/javascript">
$(document).ready(function(){
$('#myonoffswitch<?php echo $row["id"]; ?>').click(function(){
var myonoffswitch<?php echo $row["id"]; ?>=$('#myonoffswitch<?php echo $row["id"]; ?>').val();
if ($("#myonoffswitch<?php echo $row["id"]; ?>:checked").length == 0)
{
var a<?php echo $row["id"]; ?>=myonoffswitch<?php echo $row["id"]; ?>;
}
else
{
var a<?php echo $row["id"]; ?>="off";
}

$.ajax({
type: "POST",
url: "ajax.php",
data: "value="+a<?php echo $row["id"]; ?>,
success: function(html){
$("#display").html(html).show();
}
});

});
});
</script><div class="onoffswitch">
<input type="hidden" value="<?php echo $row["id"]; ?>" name="ids" />
<input type="checkbox" name="onoffswitch<?php echo $row["id"]; ?>" class="onoffswitch-checkbox" id="myonoffswitch<?php echo $row["id"]; ?>"
<?php
$query3=mysql_query("select * from mytable where id=$row[id]");
$query4=mysql_fetch_array($query3);
if($query4['text']=="off")
{
echo "checked";
}
?>>
<label class="onoffswitch-label" for="myonoffswitch<?php echo $row["id"]; ?>">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
<div id="display"></div>
</td>
                  </tr>
                  <?php
                    }
                    ?>
                </tbody>
              </table>
            </div>
  

Ajax.php

<?php
$query=mysql_connect("localhost","pma","pmapass");
mysql_select_db("testdb",$query);
if(isset($_POST['value']))
{
$value=$_POST['value'];
$id=$_POST['ids'];
mysql_query("update mytable set text='$value' where id=2");
echo "<h2>You have Chosen the button status as:" .$value."</h2>";
}
?>

1 个答案:

答案 0 :(得分:0)

为了你自己;):

  1. 请整理您的代码
  2. 如果某些内容无法从您的数据库加载,请添加输出,请在php example
  3. 中查找
  4. 为什么2.查询,您已经使用“选择* ”从“mytable”加载了所有数据?
  5. 尽管如此,这是一个基于您的代码(未经测试)的示例:

    的index.php

        <?php
    $query=mysql_connect("localhost","pma","pmapass");
    mysql_select_db("testdb",$query);
    include("connection1.php");
    $result = mysql_query("SELECT * FROM mytable ORDER BY id");
    ?>
    
    <script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
    </script>
    
    <div>
        <table id="datatables" class="display">
            <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Text</th>
            </tr>
            </thead>
            <tbody>
            <?php while ($row = mysql_fetch_array($result)) { ?>
                <tr>
                    <td><?php echo $row["id"]; ?></td>
                    <td><?php echo $row["name"]; ?></td>
                    <td><?php echo $row["text"]; ?></td>
                    <td>
                        <div class="onoffswitch">
                            <input type="hidden" value="<?php echo $row["id"]; ?>" />
                            <input type="checkbox" class="onoffswitch-checkbox"
                                <?php
                                if($row['text']=="off")
                                {
                                    echo "checked";
                                }
                                ?>>
                            <label class="onoffswitch-label" for="myonoffswitch<?php echo $row["id"]; ?>">
                                <div class="onoffswitch-inner"></div>
                                <div class="onoffswitch-switch"></div>
                            </label>
                        </div>
                        <div id="display">
                        </div>
                    </td>
                </tr>
            <?php
            }
            ?>
            </tbody>
        </table>
        <script type="text/javascript">
            $(document).ready(function(){
                $('.onoffswitch').click(function(){
                    var hiddenValueID = $(this).children(':hidden').val();
                    if ($(this).children(':checked').length == 0)
                    {
                        var valueData = 'on';
                    }
                    else
                    {
                        var valueData = 'off';
                    }
    
                    $.ajax({
                        type: "POST",
                        url: "ajax.php",
                        data: {value: valueData, id: hiddenValueID} ,
                        done: function(html){
                            $("#display").html(html).show();
                        }
                    });
    
                });
            });
        </script>
    </div>
    

    ajax.php

    <?php
    $query=mysql_connect("localhost","pma","pmapass");
    mysql_select_db("testdb",$query);
    if(isset($_POST['value']))
    {
        $value=$_POST['value'];
        $id=$_POST['id'];
        mysql_query("update mytable set text='$value' where id=$id");
        echo "<h2>You have Chosen the button status as:" .$value."</h2>";
    }
    ?>
    

    如果您从ajax请求中得到答案,可以查看您的browser-console。