0

Allright, to make everything clear I will explain everything as clear as I can and include images. It will probably be quite a long question like this but I just want to make everything clear.

Well, I curently have a "textbox" (TinyMCE AJAX File Manager) which is displaying the html from a textfile like this:

enter image description here This is the code to this atm:

Bericht:</td><td align="left"><textarea name="content" cols="50" rows="15"><?php echo "$show"?></textarea></td></tr>

And:

<?php
if (file_exists("prwyswig.txt")){
$show = file_get_contents('prwysiwyg.txt');
}
else{
$show = file_get_contents('tabel.txt');
}
?>

What I want is to not use the textfiles anymore but I want to select html codes which are stored in a MySQL database. I made a dynamic dropdown list which includes the names of "newsletters".

So what I want is to select a newsletter from the dropdown list and then load the html that belongs to that titel into the textarea. I have tried several things myself but I just can't figure out how to manage this.

Do I have to write another query or not? Do I have to put the dorpdown list in another form to be able to load the data in the textarea with a submit button?

I will post the rest of the codes and my DB table structure below because you will probably need them^^ If you have any other question just ask them in the comments, any help would be great!

NOTE: I know I shouldn't be using mysql_* but that is not the issue here. I will change to PDO later on!

Connect to DB+query to select the right data:

<?php
mysql_connect('localhost','root','root');
mysql_select_db('NAW') or die (mysql_error());
$strSQL     = "SELECT Content, Titel FROM NAW.Mail";
$sql_result     = mysql_query($strSQL);
?>

Dynamic dropdown list:

<td valign=top>Nieuwsbrief:</td>
<td>
<?php
echo "<select name=\"show\">"; 
echo "<option size =30 selected>Select</option>";
if(mysql_num_rows($sql_result)) 
{ 
while($row = mysql_fetch_assoc($sql_result)) 
{ 
echo "<option>$row[Titel]</option>"; 
} 

} 
else {
echo "<option>No Names Present</option>";  
} 
?>

The database Table:

ID  Content                 Datum       Titel
1  (lots of encoded html)   18-03-13    test
2  (lots of encoded html)   18-03-13    test2
4  (lots of encoded html)   18-03-13    alles weer testen
5  (lots of encoded html)   20-03-13    testje
6  (lots of encoded html)   21-03-13    Statusupdate week 6
Daanvn
  • 1,254
  • 6
  • 27
  • 42
  • decoded or encoded html? – hjpotter92 Mar 26 '13 at 10:46
  • [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 Mar 26 '13 at 10:47
  • ment encoded^^ and I know I shouldn't be using mysql_*, look at my note.. – Daanvn Mar 26 '13 at 10:48
  • As far as I have understood, you are unable to load database `Content` column in your `TextArea`, right? If so? have you written `OnChange` event handler for ``, if so, please share that code as well. – Muhammad Ummar Mar 26 '13 at 10:50
  • @Ummar, I have not written OnChange. Everything I have is posted in my question. – Daanvn Mar 26 '13 at 11:10
  • @Daanvn, Thanks for clarification. I will post the possible solution soon. – Muhammad Ummar Mar 27 '13 at 12:33
  • Allright, when you post it I will try is as soon as I can! Thnx in advance. – Daanvn Mar 27 '13 at 13:10
  • @Daanvn, I have posted code, have a look, hope it will resolve your issue, please keep in mind, that you will have to make certain validations and error checks before you put your code in production environment. Good Luck! – Muhammad Ummar Mar 27 '13 at 13:15

1 Answers1

1

You can do it as follows, Your HTML and PHP code for your form would be like this.

<?php
mysql_connect('localhost','root','root');
mysql_select_db('NAW') or die (mysql_error());
$strSQL     = "SELECT Titel FROM NAW.Mail";
$sql_result     = mysql_query($strSQL);
?>


    <form id="myform">
        <td valign=top>Nieuwsbrief:</td>
        <td>
        <?php
        echo "<select id=\"NieuwsbriefSelect\" name=\"show\">"; 
        echo "<option size =30 selected>Select</option>";
        if(mysql_num_rows($sql_result)) 
        { 
        while($row = mysql_fetch_assoc($sql_result)) 
        { 
        echo "<option value=\"$row[Titel]\">$row[Titel]</option>"; 
        } 

        } 
        else {
        echo "<option>No Names Present</option>";  
        } 
        ?>

    Bericht:</td><td align="left"><textarea name="content" cols="50" rows="15"></textarea></td></tr>

</form>

Use JQuery to add an onChange event for <Select> like this

<script>
// variable to hold request
var request;
$(document).ready(function() {

$('#NieuwsbriefSelect').change(function() {
  //send Ajax call to get new contents
  var selected_text = $(this).val();
  // setup some local variables
  var $form = $("#myform");
  // let's select and cache all the fields
  var $inputs = $form.find("input, select, button, textarea");
  // serialize the data in the form
  var serializedData = $form.serialize();

  // fire off the request to /get_my_contents.php
  request = $.ajax({
    url: "/get_my_contents.php",
    type: "post",
    data: serializedData
   });


   // callback handler that will be called on success
   request.done(function (response, textStatus, jqXHR){
    //populate the TextArea
    $("textarea[name='content']").html(response);
   });


});

});
</script>

And finally your get_my_contents.php would be like this

<?php
$title = $_POST["show"];
mysql_connect('localhost','root','root');
mysql_select_db('NAW') or die (mysql_error());
$strSQL     = "SELECT Content from NAW.Mail where Titel = '$title' ";
$sql_result     = mysql_query($strSQL);

$row = mysql_fetch_assoc($sql_result)
print $row["Content"];
?>

Hope it will resolve your issue.

EDIT You can try out this fiddle (Just client side code, Ajax will not work) http://jsfiddle.net/jjWdF/

Muhammad Ummar
  • 3,541
  • 6
  • 40
  • 71
  • Thnx, I will try it as soon as I can but I gtg in some mins so I will probably be able to try it tommorow! – Daanvn Mar 27 '13 at 13:14
  • I just tried to use this but it doesn't seem to work. The dropdown list still works fine but when I select something nothing happens. It does not give any errors, it just does nothing. My effords to get it to work haven't worked so far since I am not familiar with JQuery. Is it maybe an option that you can look at my whole code? – Daanvn Mar 28 '13 at 09:22
  • I am assuming that you have added file `get_my_contents.php` on your server. Put an alert in `request.done` and check if it is being fired or not. Sure I can help you on this part of your code. – Muhammad Ummar Mar 28 '13 at 09:28
  • Yes, I have added the php file to my server. I added this alert to the request.done: request.fail(function(jqXHR, textStatus) { alert( "Request failed: " + textStatus ); }); With the alert it still does nothing. – Daanvn Mar 28 '13 at 09:40
  • @Daanvn, What are the results in alert? success or failure? and what is text? – Muhammad Ummar Mar 28 '13 at 09:43
  • It does not give any alert so I would assume it is a succes. However when I put the alert in your jsfiddle it will give the error: Request failed: error. I apologize for not giving you any concrete answers but as I said I haven't got a clue how JQuery works. Also I would like to thank you for all your fast responses^^ – Daanvn Mar 28 '13 at 09:51
  • @Daanvn, Please note, I have mentioned earlier that `Fiddle` will not work with Ajax part, and you have to try it on your dev server. Put alert in `request.done()` on your development server and let me know result of that. like `alert(response);`. – Muhammad Ummar Mar 28 '13 at 10:04
  • I have added alert( "test"); to request.done and it still does nothing. – Daanvn Mar 28 '13 at 10:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27078/discussion-between-ummar-and-daanvn) – Muhammad Ummar Mar 28 '13 at 10:14