douxian3828 2012-09-20 08:15
浏览 44
已采纳

在应用程序中实现w3schools ajax示例

I am having difficulty implementing an ajax example from the W3C website. The example pulls back results from a database and displays them in a table based on which option has been selected from a Dropdown menu.

http://www.w3schools.com/php/php_ajax_database.asp

I have tried configuring this example to meet my requirements. This is to update a field in the database based on the option that is selected. I think i'm close I just wanted to make sure I am on the right track. When an option is selected an ajax call is made which triggers my php script to make the changes. It looks like this:

<?php
$q = mysql_real_escape_string($_POST['q']);
print_r($_GET["q"]);


$con = mysql_connect('localhost', '', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("Orders", $con);

$sql= "UPDATE Orders SET status='.$_POST[q]['.$i.'].' WHERE   ID='.$_POST[order_no]    ['.$i.'].'";

however this data is not entered into the database. I can retrieve the value of $q which is either 'approved' 'pending' or 'disapproved' but not the specific order number to update that individual row. My select box looks like this:

echo '<select name="order_status[]" id="id" onchange="showUser(this.value)">';
    echo '<option value = "Pending"  name="order_status['.$i.']" class =       "pending"' . ($row['status'] == 'Pending' ? ' selected=selected' : '') . '>Pending</option>';
    echo '<option value = "Approved" name="order_status['.$i.']" class = "approved"' . ($row['status'] == 'Approved' ? ' selected=selected' : '') . '>Approved</option>';
    echo '<option value = "Disapproved" name="order_status['.$i.']" class ="disapproved"' . ($row['status'] == 'Disapproved' ? ' selected=selected' : '') . '>Disapproved</option>';

this is wrapped in form which also sends an email based on which checkbox is selected. This functionality works and I can send an email to an individual row:

 <form method ="post" action="sendemail.php">
    echo '<td><input type="checkbox" name="order_selected['.$i.']"/></td>';

//in send mail.php:
    if(isset($_POST['order_selected']))
    {
    $keys = array_keys($_POST['order_selected']);

    foreach($keys as $key)
//send email 

do I need to implement the update code in the same php script? or use the same approach to loop through an array?

Many Thanks

  • 写回答

1条回答 默认 最新

  • duanpacan2583 2012-09-20 08:51
    关注

    From your description I am not exactly sure of what your problem is but from looking at your code you have a few syntax errors in your SQL statement.

    $sql= "UPDATE Orders SET status='.$_POST[q]['.$i.'].' WHERE   ID='.$_POST[order_no]    ['.$i.'].'";
    

    should be

     $sql= 'UPDATE Orders SET status="' . $_POST['q'][$i] . '" WHERE   ID="' . $_POST['order_no'][$i] . '"';
    

    Also its not clear from your code where your $i variable is coming from and your also not doing any validation on $_POST['order_no'] before using it in the SQL statement which which will leave you vulnerable to SQL injection.

    Also this example of ajax is a very old way of doing things. I would always include the jQuery library and use there jQuery.ajax() function http://api.jquery.com/jQuery.ajax/ which does all the cross browser stuff for you. It also makes it very easy to select data from DOM elements and parse them into a $_POST or $_GET. Then by using a callback function you can continue with the results from your php script.

    $('select.foo').change(function() {
         var post = {
             status: $('select.foo option:selected').val();    // get the value from a dropdown 
         }
    
        $.ajax({
           type: "POST",
           url: "some.php",
           data: post,
           success: function(returnData){
               if(returnData == 1){
                   alert('updated');
               }
          }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么