-1
原諒我,但我是新的。我有一個PHP腳本生成一個HTML選擇元素,它可以動態地填充從mySQL數據庫生成的聯繫信息的選項。聯繫信息包括名字,姓名和電話號碼。我已經創建了第二個HTML選擇元素,它是空的。獲取JavaScript變量到PHP變量
創建了兩個java腳本函數。第一個叫做addcontacts,這個函數的作用是從第一個select元素中刪除聯繫人並將他們添加到第二個select元素中。第二個javascript函數removeContacts,反之亦然。
一旦所有需要的聯繫人都添加到第二個select元素,我只想將電話號碼添加到php數組中。這個想法是,短信息被髮送到選定的號碼。
這是我的代碼,
html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sender</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<script>
function addContact()
{
var cont = document.getElementById("contacts");
var snd = document.getElementById("send");
var opt = document.createElement("option");
for (var i=0; i <cont.options.length; i++)
{
if(cont.options[i].selected == true)
{
var selOpt = cont.options[i];
opt.value = selOpt.value;
opt.text = selOpt.text;
try
{
snd.add(opt,null);
cont.remove(i,null);
}
catch(error)
{
snd.add(opt);
cont.remove(i);
}
i--;
}
}
//snd.options.add(opt);
//opt.text = name;
//opt.name = id;
}
function removeContact()
{
var cont = document.getElementById("send");
var snd = document.getElementById("contacts");
var opt = document.createElement("option");
for (var i=0; i <cont.options.length; i++)
{
if(cont.options[i].selected == true)
{
var selOpt = cont.options[i];
opt.value = selOpt.value;
opt.text = selOpt.text;
try
{
snd.add(opt,null);
cont.remove(i,null);
}
catch(error)
{
snd.add(opt);
cont.remove(i);
}
i--;
}`enter code here`
}
}
</script>
<h2>Sender- Send Message</h2>
<form method="post" action="send.php">
<fieldset id="message">
<?php
echo 'Hello '. $_SESSION['username'].' <br/>';
?>
<br/>
<label for="firstname">Please enter your name:</label>
<input type="text" id="firstname" name="firstname" /><br />
<br/>
<label for="message">Please type your message</label>
<br/>
<textarea id="message" name="message"></textarea><br />
<br/>
<br />
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="contacts"; // Database name
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$userID = $_SESSION['userID'];
$query = "SELECT contactID, firstName, secondName, phone_number FROM phone_numbers WHERE userID = $userID ";
$result=mysql_query($query);
$phone_numbers = array();
echo "<select name='contacts' id='contacts' multiple
style='width:200px'>";
while($row = mysql_fetch_array($result)) {
echo "<option value=".$row['contactID']. ">".$row['firstName'].' '.$row['secondName'].' - ' .$row['phone_number']."</option>";
}
echo "</select>";
?>
<input type="button" onclick="addContact()" value="Add"/>
<input type="button" onclick="removeContact()" value="Remove"/>
<br/>
<select name="send" id="send" multiple style="width:200px">
<input type="submit" value="Add Contact" name="addcontact" />
</select>
<br />
<br />
<label for="number"> Or type in a single number: </label>
<br/>
<input type= "text" id="number" name="number"/>
<br/>
<br />
<input type="submit" value="Send Message" name="submit" />
<input type="submit" value="Logout" name="logout" />
</fieldset>
</form>
</body>
</html>
你會被原諒:http://php.net/tutorial.forms - 是否有任何編程問題已被打開?如果是這樣,請告訴我們。 – 2013-04-30 14:54:16
可能的重複[參考:爲什麼我的Javascript中的PHP代碼不工作?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-code-in-my-javascript-不工作) – deceze 2013-04-30 14:55:22
可能重複的[如何獲取多個選擇框的值在PHP?](http://stackoverflow.com/questions/2407284/how-to-get-multiple-selected-values-of-select -box-in-php) – 2013-04-30 14:58:15