如何在ajax或php中通过检查文本框值来更新数据库值 Ajax将允许您通过javascript使用php脚本,而无需重新加载页面

如何在ajax或php中通过检查文本框值来更新数据库值 Ajax将允许您通过javascript使用php脚本,而无需重新加载页面,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,假设您有3个文件: application.js <?php include('db_connect.php'); $id = $_GET["id"]; if(isset($_POST['name'])) { $name=mysql_real_escape_string($_POST['name']); $query=mysql_query("SELECT * FROM brands WHERE name = '$name

假设您有3个文件:

application.js

<?php
    include('db_connect.php');

    $id = $_GET["id"];

    if(isset($_POST['name']))
    {
        $name=mysql_real_escape_string($_POST['name']);
        $query=mysql_query("SELECT * FROM brands WHERE name = '$name'",$link);
        $row =  mysql_num_rows($query);

    if($row==0)
    {
        echo 
        "
        <script type='text/javascript'>
            $('#logo').animate({marginLeft:'20px'},100);
            $('#logo').animate({marginLeft:'0px'},100);
        </script>
        ";
    }
    else
    {
        $database = mysql_query("SELECT * FROM brands",$link);
        $logo = $_POST['name'];
        $logo_n = $row["name"];

        if ($logo == $logo_n || $logo == strtolower($logo_n) || $logo == strtoupper($logo_n)) {

            mysql_query("UPDATE brands SET level_br = '$id' WHERE id = $id",$link);
        }
        else {
            echo "<script>alert('NO');</script>";
            echo "<script>console.log('NO');</script>";
        }
    }
    }
?>
index.html

    $(document).ready(function(){
        // add the click event to the checkbox
        $('#TheCheckbox').click(function() {

            // Looking if your checkox is checked  
            var Am_I_Checked = !$(this).is(':checked')

           // Use javascrip to send an ajax call to your php file
           var remoteFile = 'www.yourdomain.com/db_update.php';

           // Data to send to your php file, The php file will see
           // those vas in the $_GET array ...
           // This is json format use all the time in javascript if you need more info
           // google it :) .
           // http://www.w3schools.com/json/json_intro.asp
           var dataToSend = {
                'Am_I_Checked' : Am_I_Checked,
                'variable1'    : 'the value',
                'variable2'    : 'the value'
                // etc...   
           }

           // This is the fonction that will be executed once the php call is done
           // I know its look like a variable, but its a lamba style function, that
           // also exist in php is you dont know
           //
           // If you notice, the data parameter is what will be return by php ( echoed by ...)
           // In this example that will be recognize as json
           var oneAjaxIsDone = function(data) {
               //example
               alert(data.whatever); // in this example, that will alert the string : "you want to return"
           }

            $.ajax({
                dataType: "json",
                url: remoteFile,  /* the remote script to call */
                data: dataToSend, /* the json data of element that you want to send to your script */
                success: oneAjaxIsDone /*  there you pass your lamba */
                });

        })
    })

db_update.php

    <html>
        <header>
            <script src="jquery.js">
            <script src="application.js">
        </header>
        <body>
            <input id="TheCheckbox" type="checkbox">
        </body>
    </html>

重申一下,不要问您是否使用ajax或php,因为它们做的工作不同

php:更新数据库,返回答案

html:包含html和布局

javascript:bing事件到您的复选框对象并触发ajax


ajax:如果你知道的话,就把它看作是你的脑袋和服务器脚本之间的出租车司机吧

,你的问题到底是什么?看起来你是在要求我们为你写一篇ajax教程的介绍。已经有很多了。试试谷歌。我使用ajax,但通过重新加载页面来更新数据库首先,你的代码真的很混乱,混合使用php和html总是一个坏主意,你在上面添加javascript!请给我举个例子Fredic Naultt非常感谢我会尝试的
    <?php
        $ajaxVars = $_GET[];

        // Do your database stuff .....

        // Now we will build the return, remeber the ajaxIsDone function above...
        $return = array('whatever' => 'you want to return');

        // When you send data from javascript to php via ajax that was in json format
        // we have to do the sameting here
        $jsonString = json_encode($return);

        echo $jsonString;
    ?>