Jump to content

Populate another drop down list with data from database and insert into another database


jackgoddy123

Recommended Posts

Hello experts,
      I have created a dropdown list on each click of drop down another drop down list appears from drop1 database. Below is the code of the same:
 
dropdown1.php
 
<html>
<head>
<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","drop2.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a plan:</option>
<option value="1">Dedicated</option>
<option value="2">VPS</option>

</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

drop2.php:

 

 

<?php

$q = intval($_GET['q']);
 
$con = mysqli_connect('localhost','root','','test');
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }
 
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM drop WHERE id = '".$q."'";
 
$result = mysqli_query($con,$sql);
 
/*echo "<table border='1'>
<tr>
 
<th>plan1</th>
<th>plan2</th>
 
</tr>";*/
  echo "Plan";
  echo "<select>";
                                                                                     
while($row = mysqli_fetch_array($result))
  {
 
   echo "<option>" . $row['plan1'] . "</option>";
   echo "<option>" . $row['plan2'] . "</option>";
  
  }
//echo "</table>";
echo "</select>";
mysqli_close($con);
?>
 

 

The above code works perfectly fine for shown the proper dropdown.
Now i want to insert the second dropdown value in another database.
And i am quite stuck in that.  :(
 
 

 

Link to comment
Share on other sites

1. Your post is in the wrong forum.

2. I don't know where it really belongs because I am not sure I understand your request.

 

You are using AJAX on the first select list to make a call and return the contents for the 2nd select list. I think you are wanting that when the user makes a selection from the 2nd select list that it automatically inserts a record into the database. Is that correct?

 

Well, that's probably a bad idea. All it takes is for the user to make an errant click on that 2nd select list for an unwanted value to be sent. It is much better to have the user enter the form data and click a button to submit the data before committing data to the database. But, if you really want to, just create an onchange event on that second select list. Then create a function for that event to either submit the form automatically or send the value of that select list via AJAX. Then create a page to process the submission depending on the method you chose to send the data (normal POST or AJAX).

Link to comment
Share on other sites

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.