2013-04-30 5 views
-1

용서해주세요. 나는 동적으로 mySQL 데이터베이스에서 생성 된 연락처 정보로 옵션을 채우는 HTML select 요소를 생성하는 PHP 스크립트를 가지고있다. 연락처 정보에는 이름, 두 번째 이름 및 전화 번호가 포함됩니다. 나는 비어있는 두 번째 HTML 선택 요소를 만들었습니다.자바 변수를 PHP 변수로 가져 오는 중

두 개의 자바 스크립트 기능이 생성되었습니다. 첫 번째는 addcontacts라고하며이 함수의 작업은 첫 번째 select 요소에서 연락처를 제거하고 두 번째 select 요소에 추가하는 것입니다. 두 번째 javascript 함수 인 removeContacts는 반대로 수행합니다.

원하는 연락처가 두 번째 선택 요소에 추가되면 전화 번호 만 PHP 배열에 추가됩니다. 아이디어는 선택한 숫자로 SMS 메시지가 전송된다는 것입니다.

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> 
+0

용서받을 수 있습니다 : http://php.net/tutorial.forms - 프로그래밍 질문이 열려 있습니까? 그렇다면 저희에게 알려주십시오. –

+0

[참고 : 내 자바 스크립트의 PHP 코드가 작동하지 않는 이유는 무엇입니까?] (http://stackoverflow.com/questions/13840429/reference-why-does-the-php-code-in-my-javascript- 작동하지 않음) – deceze

+0

가능한 [PHP에서 선택 상자의 여러 선택된 값을 얻는 방법?] (http://stackoverflow.com/questions/2407284/how-to-get-multiple-selected-values-of-select -box-in-php) –

답변

0

PHP는 서버 측에서 실행, 내 코드입니다. JavaScript는 클라이언트 측에서 실행됩니다. 페이지가 사용자에게 표시되기 전에 PHP는 작업을 완료했습니다. 리디렉션, 새로 고침 또는 AJAX 요청을해야하지만 요점은 어떻게 든 서버로 정보를 다시 보내야한다는 것입니다.

질문에 대답하려면 ... 아니요, 자바 스크립트에서 PHP 변수를 설정할 수 없습니다.

관련 문제