Click here to Skip to main content
14,936,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys, I have a 2 level dropdown menu, and a textarea, which I would like to get populated as I choose from the dropdown menu. I managed the dropdown menu to word, but I cannot get the textarea to be populated. 2 dropdown and the textarea relate to the same data table.

The textarea has a CKEditor in it

The PHP/SQL code works. I have a trouble getting the result into the textarea.

<form>
  <div class="form-group">
    <select class="form-control" name="page" id="page" onchange="FetchArticle(this.value)" required>
      <option value="">Select page...</option>
      <?php foreach ($results as $rsp) { ?>
          <option value="<?php echo $rsp["id"]; ?>"><?php echo $rsp["page_name"]; ?></option>
      <?php } ;
          ?>
    </select>
  </div>
  <div class="form-group">
    <select class="form-control" name="article" id="article" onchange="FetchArticleBody(this.value)" required>
      <option value="">Select article...</option>
    </select>
  </div>
  <div class="form-group">
    <textarea name="article_body" id="article_body" value=""></textarea>
  </div>
</form>


<script type="text/javascript">
  function FetchArticle(id){
    $('#article').html('');
    $.ajax({
      type:'post',
      url: 'ajaxdata.php',
      data: { page_id : id},
      success : function(data){
        $('#article').html(data);
      }
    })
  }
  function FetchArticleBody(id){
    $('#article_body').html('');
    $.ajax({
      type:'post',
      url: 'ajaxdata.php',
      data: { article : id},
      success : function(data){
        $('#article_body').html(data);
      }
    })
  }
</script>


<?php
include_once 'config.php';

    if(isset($_POST['page_id'])){
      $page_id = $_POST['page_id'];
    $sql = "SELECT * FROM articles WHERE page_id=:page_id";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(":page_id", $param_page_id, PDO::PARAM_INT);
    $param_page_id = $page_id;
    $stmt->execute();
    $results = $stmt->fetchAll();
    foreach ($results as $rsp) {
      echo '<option value='.$rsp['id'].'>'.$rsp['article_name'].'</option>';
    }
  }
  if(isset($_POST['article'])){
  $id = $_POST['article'];
  $sql = "SELECT article_body FROM articles WHERE id=:article";
  $stmt1 = $pdo->prepare($sql);
  $stmt1->bindParam(":article", $param_id, PDO::PARAM_INT);
  $param_id = $id;
  $stmt1->execute();
  $results = $stmt1->fetchColumn();
  echo $results;
  }
 ?>


Any and all help is greatly appreciated!

What I have tried:

I have tried to put php fetch codes in different files. I have tried to change the id of the div with the textarea to "article_body" and put textarea in php fetch code.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900