How can i get value from selection box in javascript? - php

<table border="1">
<tr>
<th>
SR No.<br />
</th>
<th>
Name
</th>
<th>
Address
</th>
<th>
Area
</th>
<th>
City
</th>
<th>
Contact No.
</th>
<th>
Volunteer List
</th>
<th>
Bird Namea
</th>
<th>
Status
</th>
<th>
Send SMS
</th>
</tr>
<?php
while($row = mysql_fetch_array($pager->result))
{
?>
<tr>
<td>
<?php echo $row['InqID']; ?>
</td>
<td>
<?php echo $row['InqFromName']; ?>
</td>
<td>
<?php echo $row['InqFromAddress']; ?>
</td>
<td>
<?php echo $row['InqArea']; ?>
</td>
<td>
<?php echo $row['InqCity']; ?>
</td>
<td>
<?php echo $row['InqContactNo']; ?>
</td>
<td>
<select name="volunteerSelect1" id="volunteerSelect1">
<option value="" selected="selected">Please Select Volunteer</option>
<?php
$select="SELECT * FROM tran_ngovolent where ngovolentNGOCode=".$ngoSelectId;
//echo $select;
$result=mysql_query($select);
while($rowBird=mysql_fetch_array($result))
{
echo"<option value=".$rowBird['ngovolentMobile']." id='volmobile'>".$rowBird['ngovolentName']."</option>";
}
?>
</select>
</td>
<td>
<?php
$birdID=$row['InqBirdType'];
$birdSelect="SELECT Bird_Name FROM mst_bird WHERE Bird_Code=".$birdID;
$result=mysql_query($birdSelect);
while($bird_name=mysql_fetch_array($result))
{
$birdname=$bird_name['Bird_Name'];
echo $birdname;
}
?>
</td>
<td>
<?php
$inqStatus=$row['InqStatus'];
if($inqStatus==0)
{
echo "<a href='index.php?id=".$row['InqID']."&status=status&page=".$pageid."' onclick='return confirmAction()'>Pending</a>";
}else
{
echo "Finished";
}
?>
</td>
<td>
<input type="button" name="SendSMS" value="Send SMS" onclick="smsNgo(<?php echo $birdID;?>,<?php echo "'".$row['InqFromAddress']."'";?>,<?php echo "'".$birdname."'";?>,<?php echo "'".$row['InqCity']."'";?>,<?php echo "'".$row['InqArea']."'";?>,<?php echo "'".$row['InqFromName']."'";?>,<?php echo "'".$row['InqContactNo']."'";?>);return sms()"/>
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="10">
<?php echo $pager->show(); ?>
</td>
</tr>
</table>
In this Program i get value from database in my selection box. show this image
In first selection box i got perfect out put in my JavaScript but other one selection box value is null or empty Here is my JavaScript and Ajax code.
function smsNgo(birdno,address,birdname,city,area,name,contact)
{
//document.write(mobile1);
var ngocontact = document.getElementById("volunteerSelect1").value;
//var ngocontact = selectElement.options[selectElement.selectedIndex].value;
var xmlhttp;
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("smsStatus").innerHTML=xmlhttp.responseText;;
window.open("http://localhost/goal_bird/admin/edit.php","_self");
}
}
var url="http://www.****.co.in/sendsms.aspx?mobile=******&pass=******& senderid=SMSIdea&to="+ngocontact+"&msg=Bird id is "+birdno+" "+birdname+" "+name+" "+address+" "+area+" "+city+" "+contact;
document.write(url);
xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send();
setTimeout("location.href='index.php'",2000);
}

You can make the volunteerSelect1 name unique by adding some db unique id with it. May be InqID in this case?
<select name="volunteerSelect_<?php echo $row['InqID']; ?>"
id="volunteerSelect_<?php echo $row['InqID']; ?>">

From a glance, i can see the name="" for all the <select> tags are the same... "volunteerSelect1"
try using something like volunteerSelect[id] or volunteerSelect[]
The SAME thing applies to the id="" attribute, they are all using the same ID, this is used by javascript to identify a particular DOM item
However you cant use [] in the id try volunteerSelect_ID
That's probably the problem, the ID attribute

modify this <select name="volunteerSelect1" id="volunteerSelect1"> to this
<select name="volunteerSelect1" id="volunteerSelect<?php echo $row['InqID']; ?>">
then this <input type="button" name="SendSMS" value="Send SMS" onclick="smsNgo(<?php echo $birdID;?>,<?php echo "'".$row['InqFromAddress']."'";?>,<?php echo "'".$birdname."'";?>,<?php echo "'".$row['InqCity']."'";?>,<?php echo "'".$row['InqArea']."'";?>,<?php echo "'".$row['InqFromName']."'";?>,<?php echo "'".$row['InqContactNo']."'";?>);return sms()"/>
to this
<input type="button" name="SendSMS" value="Send SMS" onclick="smsNgo(<?php echo $row['InqID']; ?>,<?php echo $birdID;?>,<?php echo "'".$row['InqFromAddress']."'";?>,<?php echo "'".$birdname."'";?>,<?php echo "'".$row['InqCity']."'";?>,<?php echo "'".$row['InqArea']."'";?>,<?php echo "'".$row['InqFromName']."'";?>,<?php echo "'".$row['InqContactNo']."'";?>);return sms()"/>
then this function smsNgo(birdno,address,birdname,city,area,name,contact)
to this function smsNgo(inqid,birdno,address,birdname,city,area,name,contact)
and finally this var ngocontact = document.getElementById("volunteerSelect1").value;
to this var ngocontact = document.getElementById("volunteerSelect" + inqid).value;
Basically, what I did that i added an unique identifier to each selectbox according to inqid. Then I call function smsNgo with this ID and select the right selectbox :)

Since you tagged this as jquery. so to get the value from select dropdown, you will use something like this

Related

Cannot delete row in database using PHP

I can't seem to delete row in database by id in php
I think the the id is not passed to the $_POST['delete']
however, the popup "Your data is deleted" is displayed, but the data is not deleted.
So I'm not sure where is the error in this code.
I also try to delete the data by its id
for example: Delete book where no='4';
and the code seems to run fine because the data is deleted in the database.
<html>
<script>
function confirmDelete() {
return confirm('Are you sure?');
}
</script>
<!DOCTYPE html>
<head>
<form action="test.php" method="POST">
<br><br><br>
<table bordercolor="#FFCC66" align="center" bgcolor="#FFFFFF">
<tr>
<th>No</th>
<th>Title</th>
<th>Author</th>
<th>Year</th>
<th>Donor's Name</th>
<th>Call Number</th>
<th>Date Received</th>
<th>Handled By</th>
<th></th>
<th></th>
</tr>
<?php
include ('config.php');
$view=mysqli_query($conn,"SELECT * FROM book");
?>
<?php while($v=mysqli_fetch_array($view)){ ?>
<tr>
<td>
<?php echo $v["no"];?>
</td>
<td>
<?php echo $v["title"];?>
</td>
<td>
<?php echo $v["author"];?>
</td>
<td>
<?php echo $v["year"];?>
</td>
<td>
<?php echo $v["donorname"];?>
</td>
<td>
<?php echo $v["callnum"];?>
</td>
<td>
<?php echo $v["datereceived"];?>
</td>
<td>
<?php echo $v["handledby"];?>
</td>
<td><input type="submit" name="delete" value="Delete" onclick="return confirmDelete('Are you sure?');" /></td>
</tr>
<?php
} ?>
</tr>
</table>
<br><br>
</form>
</body>
</html>
<?php
if(isset($_POST['delete']))
{
include('config.php');
$no =$v["no"];
$d=mysqli_query($conn,"DELETE FROM `book` WHERE no='$no'");
if ($d)
{
echo "<script type='text/javascript'> alert('Your data is deleted!'); </script>";
echo"<meta http-equiv='Refresh' content='0' >";
}
else
{
echo "<script type='text/javascript'> alert('Your data cannot delete!'); </script>";
}
mysqli_close($conn);
}
?>
Change the submit element to
<td>
<input type="submit" name="delete" value="<?php echo $v['no'];?>" onclick="return confirmDelete('Are you sure?');" />
</td>
and
$no = $_POST["delete"];
Another solution si to add a hidden input with your value.
<td>
<?php echo $v["no"];?>
<input type="hidden" value="<?php echo $v["no"];?>" />
</td>
In your php you will find the value in $_POST['no']
This solution is better to pass multiple arguments in POST like a captcha or a confirmation (checkbox).
logic is not correct, while you press the delete button, all the data will be passed along with submitting because your tag is outside of the loop.
As my opinion, you should use ajax like functionality here, or follow this method.
<?php while($v=mysqli_fetch_array($view)){ ?>
<form action="test.php" method="POST">
<tr>
<td>
<?php echo $v["no"];?>
<input type="hidden" value="<?php echo $v["no"];?>" name="no" >
</td>
<td><input type="submit" name="delete" value="Delete" onclick="return confirmDelete('Are you sure?');" /></td>
</tr>
</form>
<?php } ?>
and in your post call use $no = $_POST['no']; instead of $no =$v["no"];

how to prevent the $result in mysql query from looping the wrong way

I have a problem in my following code. I want all data associated with the selected id of the dropdownlist to be viewed in the textboxes as well as inside the table.
When I ran my code, the result of the query created a repetitive result because of the data inside the database is more than 1 row.
What am I missing here? Did Inmake mistakes in the normalization of the database? Can somebody help me?
below is my code in search.php
<?php
//including the database connection file
include_once("dbconnect.php");
$query1 = "SELECT * FROM kursus";
$result1 = mysqli_query($mysqli, $query1);
$result2 = mysqli_query($mysqli, "SELECT * FROM kursus");
?>
<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","search2.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
Carian : <select id="id_kursus" name="id_kursus" onchange="showUser(this.value)">
<option>Sila Pilih</option>
<?php while($row1 = mysqli_fetch_array($result1)){
echo "<option value='".$row1['id_kursus']."'>".$row1['nama_kursus']."</option>";
}
?>
</select>
</form>
<br>
<div id="txtHint"><b>Result will be display here.</div>
</body>
</html>
below is my codes in search2.php
<html>
<head>
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','admin','tracer');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql = "SELECT
a.*,
b.*,
c.*,
a.id_kursus AS id_kursus
FROM pelatih a
LEFT JOIN kursus b ON b.id_kursus = a.id_kursus
RIGHT JOIN status_pelatih c ON c.id_status_semasa = a.id_status_semasa
WHERE a.id_kursus = '.$q.'";
$result = mysqli_query($con,$sql);
$no = 1;
while($row = mysqli_fetch_array($result)) {
echo "<table class='table1'>
<tr>
<td>Kod Kursus : </td>
<td><input type='text' value= '$row[kod_kursus]'></td>
</tr>
<tr>
<td>Nama Kursus : </td>
<td><input type='text' size='40' value= '$row[nama_kursus]'></td>
</tr>
<tr>
<td>Sesi : </td>
<td><input type='text' value= '$row[sesi_kursus]''></td>
</tr>
<tr>
<td>Batch : </td>
<td><input type='text' value= '$row[batch_kursus]'></td>
</tr>
<tr>
<td>Tempoh Kursus :</td>
<td><input type='date' value= '$row[tarikh_mula_kursus]'> Hingga <input type='date' value= '$row[tarikh_tamat_kursus]'></td>
</tr>
<tr>
<td>Program Tajaan : </td>
<td><input type='text' value= '$row[penaja_program]'></td>
</tr>
<tr>
<td>Peruntukan : </td>
<td><input type='text' value= '$row[peruntukan]'></td>
</tr>
<tr>
<td>Tarikh Kajiselidik : </td>
<td ><input type='date' value= '$row[tarikh_kajiselidik]'></td>
</tr>
<tr>
<td>Cara Kajiselidik : </td>
<td ><input type='text' value= '$row[cara_kajiselidik]'></td>
</tr>
</table>
<br>";
echo "<table class='table2'>
<tr>
<th rowspan='2'>Bil</th>
<th colspan='4'>Nama dan Butiran Pelatih</th>
<th colspan='6'>Maklumat Tracer Study</th>
</tr>
<tr>
<th>Nama Pelatih</th>
<th>No. K/P</th>
<th>No. Telefon</th>
<th>Status Semasa</th>
<th>Nama Syarikat</th>
<th>Alamat Syarikat</th>
<th>No. Telefon</th>
<th>Nama Jawatan</th>
<th>Tangga Gaji</th>
<th>Catatan</th>
</tr>";
echo '<tr>
<td>'.$no.'</td>
<td>'.$row['nama_pelatih'].'</td>
<td>'.$row['ic_pelatih'].'</td>
<td>'.$row['tel_pelatih'].'</td>
<td>'.$row['status_semasa_pelatih'].'</td>
<td>'.$row['nama_syarikat'].'</td>
<td>'.$row['alamat_syarikat'].'</td>
<td>'.$row['tel_syarikat']. '</td>
<td>'.$row['jawatan'].'</td>
<td> RM'.$row['tangga_gaji'].'</td>
<td>'.$row['catatan'].'</td>
</tr>';
$no++;
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

Changing the content of a dropdown menu from a dropdown menu select

I have some dropdown menus. If I select one of them I want to change the other's content with something that is reliable with what i selected. How can I do that ? With html and php.
For example I have one table
Year with id_year and year
Stuff with id_stuf and stuff
If I select an year in the first dropdown menu , in the other drodown menu will show only the stuff from that year .
This is my content with the dropdown menus
<div class="view">
<form name="tabel" method="post" action="insertexamen.php">
<table>
<tr>
<td>Data</td>
<td><input type="date" name="data" value="data" required="required"/><br></td>
</tr>
<tr>
<td>An</td>
<td>
<?php
$sql_year="SELECT * FROM an";
$rez_year = mysqli_query($link,$sql_year);
echo "<select name=\"year\" >";
while($year=mysqli_fetch_array($rez_year))
{
echo "<option value=\"".$year['id_an']."\">".$year['grupa']."</option>";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td>Materie</td>
<td>
<?php
$sql_mat="SELECT * FROM materii";
$rez_mat = mysqli_query($link,$sql_mat);
echo "<select name=\"mat\" >";
while($mat=mysqli_fetch_array($rez_mat))
{
echo "<option value=\"".$mat['id_mat']."\">".$mat['numemat']."</option>";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td>Profesor</td>
<td>
<?php
$sql_proff="SELECT * FROM profesor";
$rez_proff = mysqli_query($link,$sql_proff);
echo "<select name=\"proff\" >";
while($proff=mysqli_fetch_array($rez_proff))
{
echo "<option value=\"".$proff['id_prof']."\">".$proff['numep']." ".$proff['prenumep']."</option>";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td>Asistent</td>
<td>
<?php
$sql_profff="SELECT * FROM profesor";
$rez_profff = mysqli_query($link,$sql_profff);
echo "<select name=\"profff\" >";
while($profff=mysqli_fetch_array($rez_profff))
{
echo "<option value=\"".$profff['id_prof']."\">".$profff['numep']." ".$profff['prenumep']."</option>";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td>Sala</td>
<td>
<?php
$sql_room="SELECT * FROM sala";
$rez_room= mysqli_query($link,$sql_room);
echo "<select name=\"room\" >";
while($room=mysqli_fetch_array($rez_room))
{
echo "<option value=\"".$room['id_s']."\">".$room['salaa']."</option>";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td>Tip</td>
<td>
<?php
$sql_type="SELECT * FROM examen";
$rez_type= mysqli_query($link,$sql_type);
echo "<select name=\"type\" >";
while($type=mysqli_fetch_array($rez_type))
{
echo "<option value=\"".$type['id_tip']."\">".$type['tip']."</option>";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td><input name="submit" type="submit" value="Trimite"/></td>
<td><input name="reset" type="reset" value="Reset"/></td>
</tr>
</table>
Are you looking for something like that:
<html>
<title>dropdownlist</title>
<head>
<script language="Javascript" type="text/javascript" >
function choix(formulaire)
{
var j;
var i = form1.boite1.selectedIndex;
if (i == 0)
for(j = 1; j <3; j++)
form1.boite2.options[j].text="";
else{
switch (i){
case 1 : var text = new Array( "London","Manchester","Birmingham");
break;
case 2 : var text = new Array("Paris","Marseille","Lyon");
break;
case 3 : var text = new Array("Berlin","Munich","Francfort");
break;
}
for(j = 0; j<3; j++)
form1.boite2.options[j+1].text=text[j];
}
form1.boite2.selectedIndex=0;
}
</script>
</head>
<body>
<form name="form1">
<select name="boite1" onChange="choix(this.form)">
<option selected>country</option>
<option>England</option>
<option>France</option>
<option>Germany</option>
</select>
<select name="boite2">
<option selected>cities</option>
<option></option>
<option></option>
<option></option>
</form>
</select>
</body>
</html>
if you want absolutely to do it using only html and php you will need to use some ajax because php is serverside and html is slient side. so for what you asy I would recommend the code above

display the retrieved content after choose the option from drop down list in different places using ajax

I have a table which has a dynamic content.
the table contains a drop down list, where the user needs to choose an option then the displayed data will be returned using ajax.
currently it's work BUT the problem is that it's working only with the first row.
After l choose from the drop down list in the second row the result was displayed on
the first row :"/ !!
table code
<table class="bordered" >
<tr class="bordered">
<?php
$qry=mysql_query("SELECT *
FROM `order` ");
//Check whether the query was successful or not
$qry1="SELECT *
FROM `order` ";
$result=mysql_query($qry1);
if(mysql_num_rows($qry)>0){
?>
<th>delete</th><th>edit</th><th>staff info</th><th>staff</th><th>ex</th><th>phone</th><th>name</th><th>id</th><th>quantity</th><th>status</th><th>time</th><th>date</th><th>order number</th><th></th>
</tr>
<?php
while($info = mysql_fetch_array( $result ))
{
$idem = $info['emp_id'];
$q="select * from empoyee where emp_id= '".$idem."';";
$r=mysql_query($q);
?>
<tr>
<td>
<form method="POST" action="delete_asu.php">
<input type="image" src="delete1.png" width="25%" height="20%"/>
<input type="hidden" name="delete_id" value="<?php echo $info['order_num']; ?>" />
</form></td>
<td>
<?php $id=$info['order_num'];
echo '<form method="POST" action="update_asu_form.php?id='.$id.'">' ?>
<input type="image" src="settings-24-128.png" width="20%" height="15%"/>
</form></td>
<td id="txtHint"></td>
<td>
<select name="users" onchange="showUser(this.value)">
<option></option>
<?php
$sql=mysql_query("SELECT asu_name from asu_staff");
while($row = mysql_fetch_array($sql)){
echo "<option>" .$row["asu_name"]. "</option>";
}
?>
</select>
</td>
<?php
while($row = mysql_fetch_array( $r ))
{
?>
<td><?php echo $row['ex']; ?></td><td><?php echo $row['phone']; ?></td><td><?php echo $row['emp_name']; ?></td><td><?php echo $row['emp_id']; ?></td>
<?php
}
?>
<td><?php echo $info['quantity']; ?></td><td><?php echo $info['status']; ?></td>
<td><?php echo $info['time']; ?></td><td><?php echo $info['date']; ?></td>
<td><?php echo $info['order_num']; ?></td><td><input type="radio" name="choose" value="<?php echo $info['order_num']; ?>" /></td>
</tr>
<?php
}
}
else echo "<h1>no orders</h1>";
?>
</table>
The ajax code
<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","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
The php code
$q=$_GET["q"];
$sql="SELECT * FROM asu_staff WHERE asu_name = '".$q."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<table>";
echo "<tr> <td>" . $row['asu_id'] . " </td><td>staff id</td></tr>";
echo "<tr> <td>" . $row['phone'] . " </td><td>staff phone</td></tr>";
echo "</table>";
Yaaah It's work
the idea:
Alhamdullilallah it's working perfectly now :D
the problem was from the id of the where I retrieve the content
I make an incremental id and send it to the ajax , then it's work
The value that is given by the select is what is in the value="" attribute on each option. In your case you need to change the code displaying the options to:
<select name="users" onchange="showUser(this.value)">
<option value="" selected></option>
<?php
$sql=mysql_query("SELECT asu_name from asu_staff");
while($row = mysql_fetch_array($sql)){
echo "<option value=\"".$row["asu_name"]."\">" .$row["asu_name"]. "</option>";
}
?>
</select>
Try this.
p.s. you could select an id with the name in your query, then use the id in the value="" attribute. Your query in your php code could then use the id in stead of the name.

Validate Dynamic Dropdown With Ajax

I have a drop down menu of mobile brands. When a user selects a mobile brand a dynamic drop populates with the mobile model according to the mobile brand I want to validate that mobile model drop down for a blank select value. I am using ajax xmlhttp request for getting the mobile model drop down menu.
My entire code is:
Code for page with mobile brand drop down menu
<script type="text/javascript">
function validate() {
if (document.myform.step3_mobilebrand.value=="") {
document.getElementById("error1").innerHTML = (error1) ? "<img src='images/formerror.gif' height='15' width='18'>" : "";
document.myform.step3_mobilebrand.focus();
return false;
}
var myTextField = document.myform.getElementById('step3_mobilemodel').value;
if (myTextField == "") {
document.getElementById("error2").innerHTML = (error2)?"<img src='images/formerror.gif' height='15' width='18'>" : "";
document.myform.step3_mobilemodel.focus();
return false;
}
}
</script>
<?php
if (isset($_POST['submitted']) and $_POST['submitted']=="Continue") {
$mobilebrand1 = $_POST['step3_mobilebrand'];
$mobilemodel1 = $_POST['step3_mobilemodel'];
if ($mobilemodel1=="") {
echo "Please Select Mobile Model";
}
$connectiontype=$_POST['step3_connectiontype'];
$internet=$_POST['step3_internet'];
include("admin/config.php");
$sql="insert into member_detail (mobilebrand,mobilemodel,connection_type,internet) values('$mobilebrand1','$mobilemodel1','$connectiontype','$internet')";
mysql_query($sql);
}
?>
<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","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
<form name="myform" method="post" action="" onsubmit="return validate();">
<table>
<tr>
<td>Mobile Brand</td>
<td><?php
include ("admin/config.php");
echo "<select name='step3_mobilebrand' onchange='showUser(this.value)'><option value='' selected>Select Mobile Brand</option>";
$result = mysql_query ("select * from mobilebrand order by mobilebrand");
while ($adcom=mysql_fetch_array($result)) {
echo "<option value=\"".$adcom['id']."\">".htmlspecialchars($adcom['mobilebrand'])."</option>";
}
echo "</select>";?><span id="error1" style="color:red;"></span></td>
</tr>
</table>
<div id="txtHint"></div>
<table>
<tr>
<td>Connection Type</td>
<td><input type="radio" name="step3_connectiontype" value="PrePaid" checked="checked">Prepaid
<input type="radio" name="step3_connectiontype" value="PostPaid">Postpaid
<span id="error3" style="color:red;"></span>
</td>
</tr>
<tr>
<td>Have Internet On Mobile</td>
<td><input type="radio" name="step3_internet" value="Yes" checked="checked">Yes
<input type="radio" name="step3_internet" value="No">No
<span id="error4" style="color:red;"></span>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submitted" value="Continue"></td>
</tr>
</table>
</form>
and the code of getuser.php is
<table>
<tr>
<td>Mobile Model</td>
<td>
<?php
$q=$_GET["q"];
include("admin/config.php");
echo "<select name='step3_mobilemodel' id='step3_mobilemodel' ><option value=''>Select Mobile Model</option>";
$result = mysql_query ("select * from mobilemodel where brandcode='".$q."'");
while ($adcom=mysql_fetch_array($result)) {
echo "<option value=\"".$adcom['mobilemodel']."\">".htmlspecialchars($adcom['mobilemodel'])."</option>";
}
echo "</select>"; ?>
</td>
</tr>
</table>
The Output am getting is correct i just want to validate the mobile model drop down menu for blank select
For validating select you can do:
var e = document.getElementById("step3_mobilemodel");
var mobileVal = e.options[e.selectedIndex].value;
if(mobileVal == "") {
...//show your error here
}
Hope it helps
This is pretty much what Sudhir posted (+1 from me) but with an example in a fiddle: http://jsfiddle.net/5TYwH/1/ using the onchange event handler.
May be you can just add a php validation for $q.
<table>
<tr>
<td>Mobile Model</td>
<td>
<?php
$q=$_GET["q"];
echo "<select name='step3_mobilemodel' id='step3_mobilemodel'>"
if($q != ""){
include("admin/config.php");
echo "<option value=''>Select Mobile Model</option>";
$result = mysql_query ("select * from mobilemodel where brandcode='".$q."'");
while ($adcom=mysql_fetch_array($result)) {
echo "<option value=\"".$adcom['mobilemodel']."\">".htmlspecialchars($adcom['mobilemodel'])."</option>";
}}else{ echo "<option value=''>Please select a mobile first</option>";} echo "</select>";?></td>
</tr></table>

Categories