簡體   English   中英

如何通過檢查Ajax或php中的文本框值來更新數據庫值

[英]How to update database value by checking textbox value in ajax or php

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

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:知道的只是將其視為您的頭腦和服務器腳本之間的出租車司機

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM