如何通过检查ajax或php中的文本框值来更新数据库值

时间:2014-09-28 11:33:26

标签: php jquery mysql ajax

<?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>";
        }
    }
    }
?>

1 个答案:

答案 0 :(得分:0)

Ajax允许您通过javascript使用php脚本而无需重新加载页面

假设您有3个文件:

<强>的application.js

    $(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 */
                });

        })
    })

<强>的index.html

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

<强> db_update.php

    <?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;
    ?>

重申不要问你是否使用ajax或php,因为他们不做同样的工作

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

html:包含你的HTML和布局

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

ajax:因为知道只是把它看作是你的头脑和服务器脚本之间的出租车司机