Fields that are changed by AJAX do not go through the submit - php

I have a form that stopped sending some field values after I added AJAX to the mix.
<select name="showId" id="showId" onChange="getClasses('findclasses.php?showId='+this.value)">
<option value="">Select a Show</option>
<?
$sql = "select * from shows order by ShowName";
$result = mysql_query($sql);
while ($show = mysql_fetch_array($result)) { ?>
<option value="<?=$show['Id']?>"><?=$show['ShowName']?></option>
<? } ?>
</select>
<div id="classdiv"> //contents reconstructed with AJAX when the show changes above
<select id="classId" name="classId">
<option value="">Select Class</option>
</select>
</div>
When the show changes, an AJAX function is called, and the contents of the classdiv are replaced with another select field (also named classId) that has the classes associated with the show.
After I click submit, I checked the $_POST variable, and classId was not in the list but the other form fields were. Any ideas on why and how to fix it?

Please check that when the contents of classdiv are replaced by ajax the all the option with select tag having their different values or not?

Related

get value of listbox in php

I have the following code:
<td>Province</td>
<td>
<select name="prov">
<?php
$sql1 = mysqli_query($connect, "SELECT * FROM Provincetbl order by Prov_Desc desc");
while ($row1 = $sql1->fetch_assoc()){
?>
<option value="prov1"><?php echo $row1['Prov_Desc']; ?> </option>
<?php
}
?>
</select><br><br></td></tr>
<td>Distrect</td>
<td>
<select name="dist">
<?php
$sql2 = mysqli_query($connect, "SELECT * FROM Distrecttbl order by Distrect_Desc desc");
while ($row2 = $sql2->fetch_assoc()){
?>
<option value="dist1"><?php echo $row2['Distrect_Desc']; ?> </option>
<?php
}
?></select></td></tr>
There are two listboxes prov and dist. I have been trying to figure out how to read the value of either listbox (i.e get it in a variable). I have found a post which states that the value of a listbox is read after the form is posted (in the variable $_Post). However, I am dealing with a listbox which is not sending anything to the server, so why is the value of the listbox read after the form is posted?
Second, I do not have a form element in my page to begin with..?
Other posts have stated that I can read the value of listbox in a variable $prov or $dist. However I cannot seem to echo the contents of these variables to the screen.
Thank you for your help.
I think all the options in the prov listbox will have the same value that you've wrote 'prov1' and the second listbox will have dist1 in all the options
What I recommande you to do is the following :
<option value="$row1['Prov_Desc']"><?php echo $row1['Prov_Desc']; ?> </option>
and for the second one use this :
<option value="$row2['Distrect_Desc']"><?php echo $row2['Distrect_Desc']; ?> </option>

post value one dropdown to other dropdown using php mysql

include('connection.php');
$con=mysql_query("select * from province"); ?>
<select name="pid" style="float:left; width:135px;">
<option value="">Select Country</option>
<?php while($row=mysql_fetch_array($con)) {
$pid=$row['province_id'];
?>
<option value="<?=$pid?>" ><?php echo $row['province_name'] ?>
</option>
<?php } ?>
</select> -
<?php
$pid=$_POST['$pid'];
include('connection.php');
$con=mysql_query("select * from city where city_state ='$pid' ") or die(mysql_error()); ?>
<select name="cid" style="float:left; width:135px;">
<option value="">Select City</option>
<?php while($row=mysql_fetch_array($con)) {
$cid=$row['city_id'];
?>
<option value="<?=$cid?>" ><?php echo $row['city_name'] ?> <?php }?>
</option>
</select>
first table is country table, that coutry id is province_id,
second table is city, that Province id is city_state,
so, post value one to another dropdown
For this, you could use AJAX. In your country dropdown box, just echo all the countries in <option> elements.
Then attach a change event on this dropdown box using jQuery, so that it will take the user's input (I mean selected country) and do an AJAX request(jQuery has various shorthand methods for AJAX too) to fetch all the provinces belonging to this country. So this request will be sent to a PHP page on your site, that will pull the details from your db using the country id that the user selected.
Then the result from the AJAX request, which would be list of provinces with their IDs is dynamically appended to the second dropdown box(ie. for province). And for this province dropdown box also you would attach a change event to it, and do the similar thing like I mentioned above to fetch all the cities under this province, using AJAX.
Do a quick research on AJAX, in Google. There are lots of tutorials to help you.

can any one help to me to get variable from jquery and use in php

i have fllowing select list now when i select producertype onchange i want producerType value from jquery and i want to isset(producertype) with php for role and change the role type respectively as
<select id="ProducerType" name="ProducerType" style="float:left;">
<option value="Principal">Principal</option>
<option value="Producer">Producer</option>
<option value="SoleProprietor">Sole Proprietor</option>
</select>
<select id="Role" name="Role" style="float:left;">
<option value="">Agent</option>
</select>
I understand the question and the answer is going to be complex.
First you need to add onChange event to your ProducerType selectbox.
Add this at the end of your html:
<script>
$(document).ready(function(){
// attach event on change
$('#ProducerType').on('change',function(){
// call script on server to check if isset() and receive values for Role
$.get("checkproducer.php?ProducerType="+$('#ProducerType').val(),function(result){
// here the script on server returned data already
// update Role selectbox with new values
$('#Role').html(result)
})
})
})
</script>
Make sure you have jquery.js loaded as usual.
You will need to create a PHP script on server which handles the check and returns new data for your select input. Like this (save as "checkproducer.php"):
<?php
if ($_GET['ProducerType']=="Principal")
echo "<option value=1>Principal1</option>"
."<option value=2>Principal2</option>";
if ($_GET['ProducerType']=="Producer")
echo "<option value=1>Producer1</option>".
."<option value=2>Producer2</option>";
if ($_GET['ProducerType']=="SoleProprietor")
echo "<option value=1>SoleProprietor1</option>"
."<option value=2>SoleProprietor2</option>";
// etc
?>

select field in form is not sending data using php

so when i send the form with with the first option 'public' selected. the data is inserted. but when i try submitting the form with the other option selected, the ones in the for each loop. the data no longer is sent. i have inspected the elements. and they are outputting all of the correct values. and they are displaying properly. why arent they inserting into the db? when i click submit nothing happens. but when i click submit for the first option, it works fine?
<form method='POST' action='add.php'>
<select>
<option name="user_page_id" value="<?php echo $_SESSION['user_id']; ?>">Public</option>
<?php
$dis=show_groups_select_list($_SESSION['user_id']);
foreach($dis as $key=>$list){
echo '<option name="user_page_id" value="'.$list['id'].'">'.$list['username'].'</option>';
}
?>
</select>
</form>
You need to put the name attribute on the select tag, not the option tag.
<select name="user_page_id">
<?php foreach ($dis as $key => $list): ?>
<option value="...">...</option>
<?php endforeach; ?>
</select>
I know you got your answer, just some more information for those who face the same problem but that solution did not work:
I had the same problem and it turned to be made by Bootstrap!
In case you are using Bootstrap , you need to provide class attribute of select:
<select name="any_name" class="form-control">
<option value="this is what would be sent if selected"> sth </option>
</select>
for more information take a glance at this discussion too!

Can I pass Ajax var result to PHP code?

Disclaimer: It's been a while since I last wrote any code. The quality of my code is likely to be sub-par. You've been warned.
Greetings.
I am coding a basic form that uses a SELECT list I populate from my database.
However, my user needs the form to be dynamic and wants to be able to select either MasterTable1 or MasterTable2 or MasterTable3...
Instead of hardcoding the table name for the database query that populates the SELECT list, I attempted to implement a basic Ajax action (used example from w3schools)...and that's when I lost my sanity...
I can output <div id='txtHint'></div> in my page and it shows the correct table name that was picked.
But how do I pass the correct table name to my query that will populate my SELECT list???
I tried
<select name="DBFilename" id="DBFilename" size="0">
<option value="">Select Filename</option>
<?php
$sql="select distinct filename from "."<div id='txtHint'></div>";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){ ?>
<option value="<?php echo $row['filename']; ?>"><?php echo $row['filename']; ?></option>
<?php } ?>
</select>
But to no avail. This is confusing since I can do this...
...
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gettable.php?q="+str,true);
xmlhttp.send();
}
</script></head>
<body><form><select name="SrcTbl" id="SrcTbl" size="0" onchange="showTable(this.value)">
<option value="">Select Data Table</option>
<option value=""> </option>
<option value="MasterTable1">up to 31 days old</option>
<option value="MasterTable2">62 days old</option>
</select>
</form><br /><div id="txtHint"><select name="tabList"><option></option></select> </div>
</body></html>
And the name of my table will be displayed in the SELECT list 'tablist'.
How do I pass the correct table name to my query that will populate my SELECT list? Thanks!!
(Pastebin =>form code)
m8, ajax is mainly used for user experience and populating a select list is so easy mode that it shouldnt be bothered with ajax in the first place!
You should use ajax if you want to use some methods on user submitted data and create an illusion of seamless data exchange between the server and the client, and based on the return results render a corresponding view or an element of the view.
unless you load every element of the view with ajax, you should populate your html with php from the start!
<select name="DBFilename" id="DBFilename" size="whatever since style belongs to css">
<option selected="selected">Select Filename</option>
<?php
$sql="SELECT DISTINCT filename from 'wherever'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){ ?>
<option value="<?php echo $row['filename']; ?>"><?php echo $row['filename'];?>
</option>
<?php } ?>
</select>
Create a separate php script which returns a list of select options -> values depending on the table name given to it. You must remember to protect against sql injection. Then use ajax to retrieve the list and insert it into the select.

Categories