javascript - Give array elements individual IDs when taken from PHP file with AJAX -
my index.php
:
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> </head> <body> <form name="form1" method="post"> <select id="dropdown1" name="country" onchange="window.getstates()"> <option> select country</option> <option value="1">pakistan</option> <option value="2">india</option> <option value="3">usa</option> <option value="4">uk</option> </select> <input type="text" id="area" style="display: none;" size="16" placeholder=" enter value"></input> <input type="button" id="submit" style="display: none" name="submit" value="submit" onclick="submit2()"> </form> <script type="text/javascript"> function show() { document.getelementbyid('area').style.display = 'inline-block'; document.getelementbyid('submit').style.display = 'inline-block'; } function getstates() { var xmlhttp; try { xmlhttp = new xmlhttprequest; } catch(e) { xmlhttp = new activexobject("microsoft.xmlhttp"); } if (xmlhttp) { var form = document['form1']; var country = form['country'].value; xmlhttp.open("get", "http://localhost/getstates.php?country="+country, true); xmlhttp.onreadystatechange = function() { if(this.readystate == 4) { var s = document.createelement("select"); s.onchange = show; s.id = "dropdown2"; s.name = "state"; s.innerhtml = this.responsetext; if(form['state']) { form.replacechild(s, form['state']); } else form.insertbefore(s, form['submit']); } } xmlhttp.send(null); } } function submit2() { var table = document.getelementbyid("dropdown1").value; var parameter = document.getelementbyid("dropdown2").value; var value = document.getelementbyid("area").value; $.ajaxsetup({ url: "http://localhost/database.php", type: "post", }); $.ajax({ data: 'table='+table+'¶meter='+parameter+'&value='+value, success: function (msg) { alert (msg); }, error: function (xmlhttprequest, textstatus, errorthrown) { alert('error submitting request.'); } }); } </script> </body> </html>
getstates.php
:
<?php $states = array( "1" => array("parameter 1", "parameter 2", "parameter 3", "parameter 4", "select"), "2" => array("parameter 1", "parameter 2", "parameter 3", "parameter 4", "select"), "3" => array("parameter 1", "parameter 2", "parameter 3", "parameter 4", "select"), "4" => array("parameter 1", "parameter 2", "parameter 3", "parameter 4", "select") ); if(isset($_get['country'])) { $c = $_get['country']; if(isset($states[$c])) { for($i = count($states[$c]) -1; $i>=0; $i--) { echo "<option value='".$states[$c][$i]."'>".$states[$c][$i]."</option>"; } } } ?>
database.php
:
<?php header('content-type: text/html; charset=iso-8859-1'); try { if(isset($_post['table']) && isset($_post['parameter']) && isset($_post['value'])){ $table = ($_post['table']); $parameter = ($_post['parameter']); $value = ($_post['value']); $db = mysql_connect("localhost", "root", ""); $select = mysql_select_db('records', $db); $query = "insert `".$_post['table']."` (parameter,value) values ('".$_post['parameter']."','".$_post['value']."');"; mysql_query($query, $db); } } catch(exception $e) { echo 'erreur : '.$e->getmessage().'<br />'; echo 'n° : '.$e->getcode(); } ?>
what want able assign id each option in dropdown2
can send text input table. example, need first entry of second dropdown have id parameter 4
, second have id parameter 3
, third have id parameter 2
, fourth option have id parameter 1
. how can in index.php
itself?
solved:(edit)
find answer here: get selected value in dropdown list using javascript?
Comments
Post a Comment