基本的網頁傳值,以前都換頁用POST/GET居多
但是使用此方法,需要轉頁
通常都會利用一個隱藏的input表單 偷偷利用js來做傳值的動作
程式如下:
<form action='./admin/edit_feature' method='post' id='product_list'>
<input type='hidden' id='product_id' name='product_id' value=''>
<script>
function which_one(product_icon){
$('#product_id').val(product_icon);
$('#product_list').submit();
}
</script>
當妳只想要在同一頁面 LOAD資料庫裡面的資料時
就需要使用ajax
HTML
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
}
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { //傳回值是固定寫法
document.getElementById("txtHint").innerHTML = xmlhttp.responseText; //[最後]把select出來的資料 傳回前面指定的html位置
//alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET", "getuser1.php?q=" + str, true);
xmlhttp.send();
}
$(function() {
$(".showid").on("click", function(){
showUser($(this).attr('id'))
});
});
</script>
</HEAD>
<BODY>
<form>
<div name="users">
<div id="">Select a person:</div>
<div id="1" class="showid">Peter Griffin</div>
<div id="2" class="showid">Lois Griffin</div>
<div id="3" class="showid">Glenn Quagmire</div>
<div id="4" class="showid">Joseph Swanson</div>
</div>
</form>
<br>
<div id="txtHint">
<b>Person info will be listed here.</b>
</div>
</BODY>
</HTML>
PHP(getuser1.php)
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '11074');
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8 ");
mysql_select_db("gamerzone_en", $con);
$sql="SELECT * FROM tblproduct WHERE id = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['model_name'] . "</td>";
echo "<td>" . $row['product_icon'] . "</td>";
echo "<td>" . $row['specification'] . "</td>";
echo "<td>" . $row['url'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>