Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8553665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:42:16+00:00 2026-06-11T14:42:16+00:00

I am having difficulty implementing an ajax example from the W3C website. The example

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:42:17+00:00Added an answer on June 11, 2026 at 2:42 pm

    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');
               }
          }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having difficulty parsing some JSON data returned from my server using jQuery.ajax() To
I meet are having difficulty in implementing a simple event OnCheckedChanged for control checkBox
I think this should be simple but I am having some difficulty implementing it.
Im having difficulty trying to design a database structure for the following scenario: My
I'm having difficulty implementing an accordion effect on three different tables using jQuery and
I having some difficulty implementing a context menu into my android application. My first
I am having difficulty updating a table via PHP. Using MySQL prompt or a
I'm having difficulty implementing EFCodeFirst.CreateTablesOnly (changed to DontDropDbJustCreateTablesIfModelChangedStart). I have successfully pulled in the
I am having a bit of difficulty implementing the android-pulltorefresh widget by Johan Nilsson
I'm implementing my own user messaging feature in my application, but I'm having difficulty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.