javascript - Make a table based on the value of a drop down menu -


i stuck in part trying fetch data php file script in json format . new php trying learn w3schools , got stuck in http method post send data php file , display in html getting error

uncaught syntaxerror: unexpected token < in json @ position 0 @ json.parse () @ xmlhttprequest.xmlhttp.onreadystatechange

and here code had done till in front part used html , javascript :

<script>  function change_myselect(sel) {    var obj, dbparam, xmlhttp, myobj, x, txt = "";    obj = { "table":sel, "limit":20 };    dbparam = json.stringify(obj);    xmlhttp = new xmlhttprequest();    xmlhttp.onreadystatechange = function() {      if (this.readystate == 4 && this.status == 200) {        myobj = json.parse(this.responsetext);        txt += "<table border='1'>"        (x in myobj) {          txt += "<tr><td>" + myobj[x].name + "</td></tr>";        }        txt += "</table>"                document.getelementbyid("demo").innerhtml = txt;      }    };    xmlhttp.open("post", "get_email.php", true);    xmlhttp.setrequestheader("content-type", "application/x-www-form-urlencoded");    xmlhttp.send("x=" + dbparam);  }  </script>
<select id="get_email" name="get_email" onchange="change_myselect(this.value)">  <option value="">choose option:</option>  <option value="3">customers</option>  <option value="4">products</option>  <option value="5">suppliers</option>  </select>      <p id="demo"></p>

and here php file :

<?php   include("admin/include/config.php");   $id=$_post['get_email']   if(isset($_post['$id'])){   $sql=mysql_query("select * subscription id=".$id);       $returnarray = array();    while ($row=mysql_fetch_array($sql)){     $returnarray =      array("email"=>$row['email'],"creationdate"=>$row['creationdate']);        }      // return json response.      header('content-type: application/json');       echo json_encode($returnarray);          } 

can suggest me doing wrong in above script . in advance

you've error in php code, you're printing email. that's not json string.

read know more json: https://www.w3schools.com/js/js_json_intro.asp

by reading js code you're accessing name object, may need change php script follow,

<?php   include("admin/include/config.php");   $id=$_post['get_email']   if(isset($_post['$id'])){   $sql=mysql_query("select * subscription id=".$id);  // missed semi colon here.    $returnarray = array();   while ($row=mysql_fetch_array($sql)){      $returnarray = array("email"=>$row['email'],"name"=>$row['name']); // assuming column name exists.        }  // return json response. header('content-type: application/json'); echo json_encode($returnarray); } 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -