Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更新记录而不刷新/离开页面_Php_Jquery_Mysql_Ajax - Fatal编程技术网

Php 更新记录而不刷新/离开页面

Php 更新记录而不刷新/离开页面,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我有一个PHP页面,其中有一个表 该表由MySQL数据库中的记录填充。 表(外壳)的一个字段可以包含两个值:0和1 当学生被安置时字段的值为1,否则为0。 在表中,我想使用一个带有O/I的JQUERY UI按钮(类似于开关) 单击按钮时,需要在MySQL表中更新该值,JQUERY UI按钮的右侧应该显示一个图标(“checked”),如果该值为1,则图标应该消失 我想我需要ajax来完成这项工作? 有人能告诉我这是否可以做到吗?以及如何做到这一点?是的,可以做到 这里有一个例子 上面说 在服务器

我有一个PHP页面,其中有一个表

该表由MySQL数据库中的记录填充。 表(外壳)的一个字段可以包含两个值:
0
1

当学生被
安置时
字段的值为1,否则为0。 在表中,我想使用一个带有O/I的JQUERY UI按钮(类似于开关)

单击按钮时,需要在MySQL表中更新该值,JQUERY UI按钮的右侧应该显示一个图标
(“checked”)
,如果该值为1,则图标应该消失

我想我需要ajax来完成这项工作?
有人能告诉我这是否可以做到吗?以及如何做到这一点?

是的,可以做到

这里有一个例子

上面说

  • 在服务器上创建一个名为api.php的php脚本
  • 复制并粘贴下面的示例并保存:
  • 是的,可以做到

    这里有一个例子

    上面说

  • 在服务器上创建一个名为api.php的php脚本
  • 复制并粘贴下面的示例并保存:
  • 
    
    <?php 
    
      //--------------------------------------------------------------------------
      // Example php script for fetching data from mysql database
      //--------------------------------------------------------------------------
      $host = "localhost";
      $user = "root";
      $pass = "root";
    
      $databaseName = "ajax01";
      $tableName = "variables";
    
      //--------------------------------------------------------------------------
      // 1) Connect to mysql database
      //--------------------------------------------------------------------------
      include 'DB.php';
      $con = mysql_connect($host,$user,$pass);
      $dbs = mysql_select_db($databaseName, $con);
    
      //--------------------------------------------------------------------------
      // 2) Query database for data
      //--------------------------------------------------------------------------
      $result = mysql_query("SELECT * FROM $tableName");          //query
      $array = mysql_fetch_row($result);                          //fetch result    
    
      //--------------------------------------------------------------------------
      // 3) echo result as json 
      //--------------------------------------------------------------------------
      echo json_encode($array);
    
    ?>
    
    <!---------------------------------------------------------------------------
    Example client script for JQUERY:AJAX -> PHP:MYSQL example
    ---------------------------------------------------------------------------->
    
    <html>
      <head>
        <script language="javascript" type="text/javascript" src="jquery.js"></script>
      </head>
      <body>
    
      <!-------------------------------------------------------------------------
      1) Create some html content that can be accessed by jquery
      -------------------------------------------------------------------------->
      <h2> Client example </h2>
      <h3>Output: </h3>
      <div id="output">this element will be accessed by jquery and this text replaced</div>
    
      <script id="source" language="javascript" type="text/javascript">
    
      $(function () 
      {
        //-----------------------------------------------------------------------
        // 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
        //-----------------------------------------------------------------------
        $.ajax({                                      
          url: 'api.php',                  //the script to call to get data          
          data: "",                        //you can insert url argumnets here to pass to api.php
                                           //for example "id=5&parent=6"
          dataType: 'json',                //data format      
          success: function(data)          //on recieve of reply
          {
            var id = data[0];              //get id
            var vname = data[1];           //get name
            //--------------------------------------------------------------------
            // 3) Update html content
            //--------------------------------------------------------------------
            $('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname); //Set output element html
            //recommend reading up on jquery selectors they are awesome 
            // http://api.jquery.com/category/selectors/
          } 
        });
      }); 
    
      </script>
      </body>
    </html>