Help with retrieving content from php to jquery

Codes here !

Moderators: macek, egami, gesf

Novica89
New php-forum User
New php-forum User
Posts: 7
Joined: Fri May 18, 2012 5:34 am

Help with retrieving content from php to jquery

Postby Novica89 » Tue May 29, 2012 12:07 pm

Hey people,I have a problem. Since I'm building simple CMS for my website and now I am on the "updating content" part and the idea is that the admin has an option to select which database entry he wants to change (drop down select list), which i managed to insert perfectly.
Now,when the admin picks a subject from the dropdown list,I am using jquery to respond to the "change" event of the select html element and when some subject is chosen,it should transfer the data to a php file which should read the colum info from the database related to that selected subject and then return everything which is in those fields and put them inside the input fields as their values so that,if just some simple change is needed,admin doesn't need to type everything from the start,but just change what needs to be changed and click on submit,which updated those columns in the database.
The problem comes when i want to insert the retrieved data as values to the input fields for the choosen subject. I am using the $.post method to transfer to the php file and the callback function then to insert the retrieved data from the database inside the input fields as their values. I don't know if this can be done this way,so I am asking for your help. The code works correctly only I cant retrieve variables from that php file which is essential to the success of this plan. My code is like this :

The code that creates the form which will be used to update the content. The default values of the input fields should change after the query is run (after the user chooses the subject on select list)

Code: Select all

<h2>Edit content</h2>
      <form action="" method="POST" id="update_content">
         <label for="description_select">What do you want to change?</label>
         <select id="description_select" name="category_select">
         
         <?php //lists all the categories as select options
            $select_descriptions = "SELECT `short_desc` FROM `link_details`";
            $run = mysql_query($select_descriptions);
            
            while($result = mysql_fetch_array($run)) {
               $link_description = $result['short_desc'];
               echo "<option>$link_description</option>";
            }            
         ?>
         
         </select><br/><br/>
         
         <input type="text" id="retrieve_link" name="retrieve_link" value="link of the tutorial"/><br/>
         <input type="text" id="retrieve_short_desc" name="retrieve_short_desc" value="short description"/><br/>
         <textarea id="retrieve_full_desc" name="retrieve_full_desc" value="full link description"/></textarea><br/>
         <input type="text" id="retrieve_image" name="retrieve_image" value="image name"/><br/>
         <input type="submit" id="submit" name="submit" value="Save"/>
         <div class="clear"></div>
         
      </form>


Then the jQuery code which handles things after the option is selected inside the select element :

Code: Select all

$('form#update_content #description_select').change(function() {
      /* $('p#updated_feedback').remove(); */
      var description = $('#description_select').val(),
         retrieve_link = $('#retrieve_link').val(),
         retrieve_short_desc = $('#retrieve_short_desc').val(),
         retrieve_full_desc = $('#retrieve_full_desc').val(),
         retrieve_image = $('#retrieve_image').val();
      $.post('../inc/updateBase.php', { description_select:description},function(data) {
         /* $('#retrieve_link').attr('value',data); */
         alert(data);
      });      
   });


And finaly the php file which should return whatever is already in the base related to the choosen subject and then the upper jQuery code should take those values and input then inside the input fields as their values:

Code: Select all

if (isset($_POST['description_select']) ) {
   $description = $_POST['description_select'];
   
   
   $retrieveData = " SELECT `links_category`,`link`,`short_desc`,`link_description`,`image` FROM `link_details` WHERE `link_description`='$description'";
   if ($query_run = mysql_query($retrieveData)) {
      while($dataRetrieved = mysql_fetch_array($query_run)) {
         $link_category = $dataRetrived['links_category'];
         echo $link = $dataRetrived['link'];
         $short_desc = $dataRetrived['short_desc'];
         $full_desc = $dataRetrived['link_description'];
         $image = $dataRetrived['image'];
         
      }
   } else {
      echo 'There has been a problem with updating database,please try again.';
   }
}

Novica89
New php-forum User
New php-forum User
Posts: 7
Joined: Fri May 18, 2012 5:34 am

Re: Help with retrieving content from php to jquery

Postby Novica89 » Fri Jun 01, 2012 8:52 am

Problem solved......


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 0 guests