我正在创建一个网站,我想根据单击哪个按钮将不同的查询加载到页面上。
我可以这样吗?
HTML:
<div id="Proceed">
4 Projects have been suggested to proceed.
</div>
<div id="result">
<!-- The title of the projects will be loaded here -->
</div>
<button id="foo"> Search </button>
javascript:
$('#foo').on('click' function(){ //the button
var x = $(this).find('div').attr('id'); // get the id
$.ajax({
url: 'profile/inbox',
type: 'POST',
data: { id: x },
success: function(res){
('#result').html(res);
}
})
})
在profile.php上:
function Inbox(){
$id = $_POST['id'];
$query = $this->db->query("SELECT `title` FROM `table` WHERE id=?",$id);
$load = $query->result_array();
$this->d['load'] = $load;
}
编辑:我添加了一些html来显示计划将查询结果加载到的位置。
答案 0 :(得分:0)
当您要使用PHP端发布的变量时,必须使用 $ _POST / $ _ REQUEST个方法$ id = $ _POST ['id'];这将起作用
答案 1 :(得分:0)
例如:(这是您的代码,我只是稍作修改,因为 例子)
<div id="Proceed"> <select id="city" name="city"> <option value="">City:</option> <option value="glasgow">Glasgow</option> <option value="london">London</option> </select> </div> <div id="result"> </div> <button id="foo"> Search </button>
脚本:(例如:)
$("#foo").click(function() { $.ajax({ url: 'profile/inbox', //Pass this value to the corresponding URL type: 'POST', dataType: "html", data: { "city": $('#city').val(), //Here request the value for id name is city }, success: function(response){ $("#result").html(response); //Retrieve value form URL to pass the view page and value to set in <div id="result"> } }); });
这是我的网址页面(您只看到那里的过程。。这是 例如)
// Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT * FROM entries"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - City: " . $row["city"]. "<br>"; } } else { echo "0 results"; } conn->close(); ?>