开发者

How to use the select menu value in the specific DIV jQuery

开发者 https://www.devze.com 2023-03-10 14:43 出处:网络
I have the following code to get the selected item value and post it to a DIV. That is okay, it is working fine, my DIV shows the selected value. But my question is how can I use that value in the 开发

I have the following code to get the selected item value and post it to a DIV. That is okay, it is working fine, my DIV shows the selected value. But my question is how can I use that value in the 开发者_开发技巧DIV ? so that I can create a php msyql query.

<head>
<style>
.response {
    padding:10px;
    background-color:#9F9;
    border:2px solid #396;
    margin-bottom:20px;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function(){
$("#developer").change(onSelectChange);
});
function onSelectChange(){
var selected = $("#developer option:selected");
var output = "";
if(selected.val() != 0){
output = selected.val();
}

$("#output").html(output);
$('#output').slideDown("slow");
}
</script>
</head>

<select id="developer">
<option value="0">Select</option>
<option value="1">name</option>
<option value="2">name2</option>
</select>

<div align=center class=response id=output style="display:none;">
<?php
$sql = "SELECT * FROM users WHERE name='$name'";
$result = mysql_query($sql) or die("err");
while($row=mysql_fetch_array($result)){
$age = $row[age]; }
echo $age;
?>
</div>


Javascript executes on the front-end, in the user's browser. PHP and mysql are executed on the backend, on your server. If you want to use values from your javascript in PHP and mysql, you will need to make a request to the back-end.

You should read up on

http://api.jquery.com/jQuery.ajax/

and/or

http://api.jquery.com/jQuery.post/


The answer to your question is that you submit the form or send an ajax request back to your server. Your server using PHP, in your case, uses that data to form the DB query.

I think that you are mixing up the timing for when this code is being run.

  1. PHP process and sends to Html loads
  2. Javascript runs (hopefully after
  3. HTML loads) Request is submited from
  4. HTML or Javascript Back to the top


I think you can use $("#output").text(output) instead of $("#output").html(output). Then you can use $("#output").text(output) to get the value your need.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号