我必须加载一个php文件内的'div'在我的html代码。为此,我使用了'required_once('filename.php'),它可以工作。
我尝试了JQuery的方式,
$("#btnSubmit").click(function(){
$("#earth").load('filename.php');
});
});
谁能告诉我一种方法来发送数据(下拉框的值)到'filename.php',然后加载'filename.php'内的div与id='earth' ?
这是我的HTML代码
<body>
<div class="row">
<form class="form-inline" action="" method="post">
<div class="btn-group">
<a class="btn btn-default dropdown-toggle btn-select" data-toggle="dropdown" href="javascript:;">
Select Country/City
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="javascript:;">Africa</a></li>
<li><a href="javascript:;">London</a></li>
<li><a href="javascript:;">South Africa</a></li>
<li><a href="javascript:;">Korea</a></li>
<li><a href="javascript:;">Delhi</a></li>
<li><a href="javascript:;">Las Vegas</a></li>
</ul>
</div>
<div class="btn-group">
<button type="submit" id="btnSubmit" class="btn btn-green">Read</button>
</div>
</form>
</div>
<div class="row">
<div class="col-md-12">
<div class="portlet box blue">
<div class="portlet-body">
<div class="dataTable_wrapper">
<div id="earth">
<?php require_once 'filename.php'; ?> (1) <-- Pass value to this file
</div>
</div>
</div>
</div>
</div>
</div>
这是文件名。php
<?php
HERE I WANT TO GET THE VALUE SELECTED IN THE DROPDOWN i.e Africa/London etc?
$selectedCity = ??
echo "<h2> Hello User! </h2>";
echo "<p> You selected ".$selectedCity."</p>";
?>
试试这样
[akshay@localhost tmp]$ cat test.html
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<div class="row">
<form class="form-inline" action="" method="post">
<div class="btn-group">
<a class="btn btn-default dropdown-toggle btn-select" data-toggle="dropdown" href="javascript:;">
Select Country/City
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="javascript:;">Africa</a></li>
<li><a href="javascript:;">London</a></li>
<li><a href="javascript:;">South Africa</a></li>
<li><a href="javascript:;">Korea</a></li>
<li><a href="javascript:;">Delhi</a></li>
<li><a href="javascript:;">Las Vegas</a></li>
</ul>
</div>
<div class="btn-group">
<button type="submit" id="btnSubmit" class="btn btn-green">Read</button>
</div>
<input type="hidden" id="country" name="country" />
</form>
</div>
<script>
$(document).ready(function(){
$(".dropdown-menu li a").click(function()
{
$("#country").val($(this).text());
});
$("#btnSubmit").click(function()
{
$.ajax({
url:"filename.php",
type:"POST", data : $('.form-inline').serialize(),
success : function(data){ alert(data); },
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.'n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.'n' + jqXHR.responseText);
}
}
});
return false;
});
});
</script>
</html>
服务器端
[akshay@localhost tmp]$ cat filename.php
<?php
// HERE I WANT TO GET THE VALUE SELECTED IN THE DROPDOWN i.e Africa/London etc?
if(isset($_POST['country']) && $_POST['country'] != '')
{
$selectedCity = $_POST['country'];
echo "<h2> Hello User! </h2>";
echo "<p> You selected ".$selectedCity."</p>";
}else
{
echo "<h2> Hello User! you didn't select anything </h2>";
}
?>
使用以下代码:
$("#btnSubmit").click(function(){
$("#earth").load('complete url to the filename.php');
});
});
然后使用你得到的结果来传递你想传递的值