Jump to content

problem populating textarea with stored data (php, MySQL and Ajax)


Recommended Posts

HI There, i have a drop-down box that calls data from MySQL, when the user selects from this it then populates a <textarea> with stored data from a column within a MySQL row, however the problem is that it is populating the textarea with all the rows rather than the content of the one selected.. here is the code

 

1) code for the drop-down box

$query = "SELECT id,output, tempname FROM templates ORDER BY output";
  $result = mysql_query($query);
  
  while ($row = mysql_fetch_array($result))
  {
      $value = $row["id"];
      $output = $row["output"];
      $tempname = $row["tempname"];
      $pairs["$value"] = $tempname;
  }
  
  echo create_dropdown("create_output", $pairs, "Choose Your Template");


  function
  create_dropdown($identifier,$pairs,$firstentry)
     {
         // start dropdown list with the <select> element and title
         $dropdown = "<select name=\"$identifier\" method=\"post\" onchange=\"showtemplate(this.value)\">";
         $dropdown .= "<option name=\"\">$firstentry</option>";
         
         // Create the elements for inside the drop down box
         foreach($pairs AS $value => $tempname)
         {
             $dropdown .="<option name=\"$value\">$tempname</option>";
         }
         
         //end the dropdown function and return it as html
         echo "</select>";
         return $dropdown;
     }
      
?>
<br />
<textarea cols="80" rows="20" name="content" style="background-color:#bed9ee;" id="content">
</textarea>

 

2) code that pulls the data from MySQL to populate the textarea

$query = "SELECT output FROM templates ORDER BY id"; 
     
$result = mysql_query($query);
while ($row = mysql_fetch_object($result))
{
echo $output = $row->output;
}

 

the ajax code seems to be working as the data (allbeit too much) is being populated, hope someone can point me in the right direction as this has been driving me slowly insane now for almost 3 days..

try this

function
  create_dropdown($identifier,$pairs,$firstentry)
     {
         // start dropdown list with the <select> element and title
         $dropdown = "<select name=\"$identifier\" method=\"post\" onchange=\"showtemplate(this.value)\">";
         $dropdown .= "<option value=\"\">$firstentry</option>";
         
         // Create the elements for inside the drop down box
         foreach($pairs AS $value => $tempname)
         {
             $dropdown .="<option value=\"$value\">$tempname</option>";
         }
         
         //end the dropdown function and return it as html
         echo "</select>";
         return $dropdown;
     }
      

 

Thanks for taking the time out to help LeadingWebDev i tried the code provided by yourself in your post and the same problem remains that the text area is populated with all the rows from the selected column rather than just the row which is selected..

over the last couple of days i must have tried many permutations, worn out my php manual almost to get this thing working properly but it just wont have it at all...  :shrug:

the <textarea> that is to be populated will be the CKeditor (if everything works ok that is). I have a table (named templates) on the database that contains

 

id

tempname =(the name in the drop-down list)

output = (stored information to populate the CKeditor textfield on select)

 

the plan is that the user selects from the dropdown and the CKeditor textarea is then populated with the output field from MySQl, this is then edited and on submit is then stored in another table for retrieval as html source when needed..

Assuming the HTTP request that AJAX makes does send the correct value/id, you would need to use that value in a WHERE clause in your query so that only the corresponding row would be returned and output in the AJAX response.

 

What method does your AJAX use to send values, GET or POST, and what is the name of the get/post parameter that is used?

@PFMaBiSmAd

you will not believe this, i commented out the ajax parameter (which is a GET request) to try fixing things another way, i have just removed the comment and ran it again and its gone and worked perfectly, im inclined to think that maybe LWD's code snippet earlier on in this thread may have helped because the last few hours have been an absolute pain, thankfully now that is over and i can sleep tonight..phew..thanks guys

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.