Jump to content

insert into mysql


kvnirvana

Recommended Posts

The user can choose values from a drop down. When the user chooses a field underneath it will display a rating box generated from the selected value from mysql, so it’s depending on what the user chooses from the drop down list.

 

If the user chooses chair it should insert chair into the field ‘toto’ along with the values name and address, so the field will automatically generate. How can that be done.

 

This is the code

<script type="text/javascript"> 
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","des.php?q="+str+"&name=<?php echo $_GET['name']; ?>"+"&pr=<?php echo $_GET['pr']; ?>"+"&id=<?php echo $_GET['id']; ?>"+"&be=<?php echo $_GET['be']; ?>",true); 
xmlhttp.send(""); 
} 
</script> 




<?php 




if (isset($_POST['submit'])) 
{ 

  search();       //call the search function 

}else{ 

   

//end if 

/*------------------------------------------------------------------------ 
            show the search form 
------------------------------------------------------------------------*/ 


  
   


echo " 
<h1><P ALIGN='center'>  Rate {$_GET['be']},  
{$_GET['name']} </P></h1> <p> 
<br><br> 
        <tr> 

<form> 
<select name='select1' onchange='showUser(this.value)'> 
<option value='book'>Book</option> 
<option value='chair'>chair</option> 
<option value='sun'>Sun</option> 
</select> 


<div id='txtHint'></div> 
</form>"; 


} 


function dropdown($field, $table) 
{   
  //initialize variables 
  $oHTML  = ''; 
  $result = ''; 
   
  //check to see if the field is passed correctly 
  if (($field == "")||($table == "")) 
  { 
    die("No column or table specified to create drop down from!"); 
  } 

  $sql = "select distinct($field) from $table"; 
   
  //call the db function and run the query 
  $result = conn($sql); 

  //if no results are found to create a drop down return a textbox 
  if ((!$result) ||(mysql_num_rows($result)==0)) 
  { 
    $oHTML .= "<input type='text' name='$field' value='' size='15'>"; 
  }elseif (($result)&&(mysql_num_rows($result)>0)){ 
     
    //build the select box out of the results 
   
    while ($rows = mysql_fetch_array($result)) 
    { 
      $oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n"; 
    } 
    $oHTML .= "</select>\n"; 
  } 
   
  //send the value back to the calling code 
  return $oHTML; 
} 

Link to comment
Share on other sites

It's not really clear to me what you want to populate.  I see the option in your form named chair but then you say it needs to populate the toto field and I don't even see that anywhere.  I would suggest you start with the tutorial on w3schools here:

 

http://www.w3schools.com/php/php_ajax_database.asp

 

 

It demonstrates using AJAX to invoke a PHP script that returns data from a table in MySQL.  It even matches up with what you are trying to do which is to change the output based off the selection of a downdown menu!

 

 

Do you have something that is successfully returning value from PHP/MySQL already?  If not, I suggest you get a simple example working.  Then we can tweak it to do what you want.

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.