我有以下腳本:設定輸入值在PHP中使用值從jQuery的
編輯:
<?php
session_start();
//connect to database
function connect() {
$dbh = mysql_connect ("localhost", "d", "a") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db("PDS", $dbh);
return $dbh;
}
if(isset($_SESSION['username'])){
$dbh = connect();
$id = $_SESSION['id'];
$sql="SELECT a.athleteId, a.fName, a.lName FROM Athletes a, SupportStaff s, StaffAthletes sa WHERE sa.staffId = $id AND a.athleteId = sa.athleteId";
$result=mysql_query($sql);
$ids = array();
$options="";
$i = 1;
while ($row=mysql_fetch_array($result)) {
$ids[]=$row['atheleteId'];
$f=$row["fName"];
$l=$row["lName"];
$options.="<OPTION VALUE=\"$i\">".$f.' '.$l."</OPTION>";
$i++;
}
$array = "";
for($x = 0; $x < count($ids); $x++)
{
$array .= "[" . $ids[$x][0] . ", ". $ids[$x][1] . "]" ;
if($x+1 < count($ids))
$ids .= ", ";
}
$idsArray = "new Array(" . $array . ")";
echo("<script>");
echo("var $ids = ".$idsArray);
echo("</script>");
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script src = "jQuery.js"></script>
<script>
$(document).ready(function(){
$("#go").click(function(e){
if($("#selectathlete option:selected").val() == "0"){
alert("Please Select An Athlete");
}else{
//set hidden textfield to id of selected athlete
//NEED TO ACCESS $ids ARRAY HERE
$("form#athleteselect").submit();
}
});
});
</script>
<title>Personal Diary System - Home Page</title>
</head>
<body>
<h1>Home Page</h1>
<p>Select An Athlete:
<SELECT ID ="selectathlete" NAME="athletes">
<OPTION VALUE="0">Choose</OPTION>
<?php echo($options);?>
</SELECT>
</p>
<form id = "athleteselect" name="athleteselect" action="viewathleteprofile.php" method="post">
<input type = "hidden" name = "hidden" id = "athleteId" value = "">
<input type = "button" name = "go" id = "go" value = "Go"/>
</form>
</body>
</html>
<?php } ?>
我有一個ID =「athleteId一個隱藏的文本框」我有一個PHP的數組,它包含在按下開始按鈕(即在$("#go").click
事件處理程序,我想分配給隱藏的文本字段值
你可以這樣做:
$("#athleteId").text() = <?php echo("$ids[4]") ?>
請將您剛剛張貼在PHP,因爲輸出的HTML更容易PHP在這裏並沒有真正的相關 – GBa
考慮到我無法集中注意力,甚至在合理的時間內找到註釋行,所以需要一個更好的方法來做到這一點 –
由於您在這個大混亂中只引用了一個PHP變量,爲什麼你不只是輸出所有的JavaScript作爲普通輸出和'<?php echo $ the_one_variable; ''裏面呢? –