原谅我,但我是新手。我有一个 php 脚本,它生成一个 HTML 选择元素,该元素使用从 mySQL 数据库生成的联系信息动态填充选项。联系信息包括名字、名字和电话号码。我创建了第二个 HTML 选择元素,它一开始是空的。
创建了两个 java 脚本函数。第一个称为 addcontacts 该函数的工作是从第一个选择元素中删除联系人并将它们添加到第二个选择元素中。第二个 javascript 函数 removeContacts 则相反。
将所有所需的联系人添加到第二个选择元素后,我只想将电话号码添加到 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>