admin管理员组文章数量:1320600
How to get the value of drop down list in ajax in php?
PHP code:
<select name="Department" id="Department" class="DropDown">
<?php
$sql = "select * from department_details";
$exec = mysql_query($sql);
echo "<option value='0'>Select</option>";
while($row = mysql_fetch_array($exec))
{
?>
<?php echo "<option value=\"".$row['DepartmentId']."\">".$row['DepartmentName']."</option> \n "; ?>
<?php } ?>
<option value="other">Other</option>
</select>
JavaScript Code:
<script>
$("#form").submit(function(event) {
event.preventDefault();
var $form = $( this ),
value1 = $form.find( 'input[name="ProfessorName"]' ).val(),
value2 = $form.find( 'input[name="Department"]' ).val(),
url = $form.attr( 'action' );
var posting = $.post( url, { ProfessorName: value1, Department: value2 } );
posting.done(function( data ) {
$( "#result" ).empty().append( data );
});
});
</script>
How to get the value of department from the drop down list using ajax without page refresh.
How to get the value of drop down list in ajax in php?
PHP code:
<select name="Department" id="Department" class="DropDown">
<?php
$sql = "select * from department_details";
$exec = mysql_query($sql);
echo "<option value='0'>Select</option>";
while($row = mysql_fetch_array($exec))
{
?>
<?php echo "<option value=\"".$row['DepartmentId']."\">".$row['DepartmentName']."</option> \n "; ?>
<?php } ?>
<option value="other">Other</option>
</select>
JavaScript Code:
<script>
$("#form").submit(function(event) {
event.preventDefault();
var $form = $( this ),
value1 = $form.find( 'input[name="ProfessorName"]' ).val(),
value2 = $form.find( 'input[name="Department"]' ).val(),
url = $form.attr( 'action' );
var posting = $.post( url, { ProfessorName: value1, Department: value2 } );
posting.done(function( data ) {
$( "#result" ).empty().append( data );
});
});
</script>
How to get the value of department from the drop down list using ajax without page refresh.
Share Improve this question asked Dec 18, 2013 at 10:53 user2281511user22815113 Answers
Reset to default 3Using jquery you can try this
value2 = $("#Department").val();
To get the select value, change :
value2 = $form.find( 'input[name="Department"]' ).val(),
To
value2 = $form.find( 'select[name="Department"]' ).val(),
And because you have an id on it, you can directly make
value2 = $form.find( '#Department' ).val(),
You used input selector, but Department is a select element.
Don't forget to make your function return false;
, in order to prevent the form from being submitted.
As you are using ajax, Use Jquery's Serialize
With this, you can create a text string (out of form field values) in standard URL-encoded notation, handy enough to make ajax calls.
本文标签: javascriptHow to get the value of drop down list in ajax in phpStack Overflow
版权声明:本文标题:javascript - How to get the value of drop down list in ajax in php? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742081328a2419715.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论