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 143351

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:04:53+00:00 2026-05-11T08:04:53+00:00

I would like to submit information to a mySql database using php and ajax.

  • 0

I would like to submit information to a mySql database using php and ajax.

The page that the info is being sent from (form.php) has multiple forms that are generated from a ‘while()’ loop.

On success, I would a response to update a div above the particular form from which the data was submitted.

I am currently using jQuery and the jquery form plugin.

I have been successful in getting the data to the database, however I am having trouble with the response being sent back to the proper div. I have been successful in getting a response back to a div that is outside of the while() loop. I have not, however, been successful in getting a response back to a div within the loop. I have placed in the code below a div called: ‘> Where I would like the note to be placed.

I know that this has everything to do with my javascript function:

<script type='text/javascript'> jQuery(document).ready(function() {     jQuery('form').ajaxForm({         target: '#noteReturn',         success: function() { $('#noteReturn').fadeIn('slow'); }     }); }); </script> 

The #noteReturn function does not specify which businesses div it should be placed in.

I hope that this makes sense.

Thank you for your help.

The code is below:

<!-- the form.php page --> <script type='text/javascript' src='js/jquery.min.js'></script> <script type='text/javascript' src='js/forms.js'></script>  <script type='text/javascript'>  jQuery(document).ready(function() {      jQuery('form').ajaxForm({      target: '#noteReturn',     success: function() {      $('#noteReturn').fadeIn('slow'); }      });  }); </script>   <?php $query = mysql_query('SELECT * FROM businesses'); while( $row = mysql_fetch_assoc( $query ) ):     $b_id = $row['bid']; ?>  <div class='notes'>   <?php   // query the db for notes associated with business... return notes texts and notes dates $notesQuery = mysql_query('SELECT business_id, notes, messageDate FROM notes WHERE notes.business_id = $b_id ORDER BY messageDate'); while( $NoteRow = mysql_fetch_assoc( $notesQuery ) ) {  extract($NoteRow);  echo '$notes<br/><span class='noteDate'>$messageDate</span><br />';  }  // end while$notesQuery ?>  <!-- this is where i would like jQuery to return the new note --> <div id='noteReturn<?php echo $b_id; ?>'></div>   <!-- begin note form --> <form name='noteForm' action='notesProcess.php' method='post'>     <input type='text'  name='note' />     <input type='hidden' value='<?php echo $b_id ?>' name='bid' />     <input type='submit' class='button'  value='Send' /> </form>  </div> <!-- end div.notes -->  <?php endwhile; ?>   <!-- ///////////////////////////////////////////////////// The page that the form submits to is this (notesProcess.php):  ///////////////////////////////////////////////////// --> <?php $note = $_POST['note']; $id = $_POST['bid']; $sql = 'INSERT INTO notes (business_id, notes) VALUES ('$id', '$note')'; $result = mysql_query( $sql ); if( $result ) { echo ' $note'; } ?> 
  • 0 0 Answers
  • 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. 2026-05-11T08:04:54+00:00Added an answer on May 11, 2026 at 8:04 am

    Change this code:

    jQuery('form').ajaxForm({      target: '#noteReturn',     success: function() {          $('#noteReturn').fadeIn('slow');     }  }); 

    To this:

    jQuery('form').ajaxForm({      target: '#noteReturn',     dataType: 'json',     success: function(data) {          $('#noteReturn' + data.id).html(data.note).fadeIn('slow');     }  }); 

    And this code:

    <?php $note = $_POST['note']; $id = $_POST['bid']; $sql = "INSERT INTO notes (business_id, notes) VALUES ('$id', '$note')"; $result = mysql_query( $sql ); if($result) {     echo " $note"; } ?> 

    To this:

    <?php $note = mysql_real_escape_string($_POST['note']); $id = mysql_real_escape_string($_POST['bid']); $sql = "INSERT INTO notes (business_id, notes) VALUES ('$id', '$note')"; $result = mysql_query( $sql ); if($result) {     print json_encode(array("id" => $id, "note" => $note)); } ?> 

    What happened?

    The change to the PHP code is making use of PHP’s json_encode function to print out the id of the business to which the note was added as well as the actual note text. In the javascript code, I added the dataType of ‘json’ to tell the script what format of response to expect. Once the request is received in the success callback, the data variable is an object with the values we passed through json_encode. So data.id has the business id and data.note has the new note. Using jQuery’s html() manipulation function, the inner html of the div is updated to the latest note. The div selector uses the id we passed, so we can update the corresponding div.

    Also, this is slightly off topic, but make sure you always use mysql_real_escape_string when putting values into a query like you are. If you do not use this, your queries will be vulnerable and susceptible to injection attacks, and they are not pretty. If a customer decided to enter a note value of ');DROP TABLE businesses; you’d really feel the pain. Preferably, switch to PDO or MySQLi and use prepared statements, as they are the ‘correct’ way of doing queries nowadays.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 111k
  • Answers 111k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In cases like this, I tend to write a class… May 11, 2026 at 9:46 pm
  • Editorial Team
    Editorial Team added an answer that is a bug, that comes from the browser ...… May 11, 2026 at 9:46 pm
  • Editorial Team
    Editorial Team added an answer You need to invoke hasLayout on the outer div to… May 11, 2026 at 9:46 pm

Related Questions

I would like to catch and log MySQL warnings in Python. For example, MySQL
I've been asked to create a fairly straightforward website for a friend. Essentially a
I am trying to send a php script some content to be stored in
I have an application that supplies long list of parameters to a web page,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.