告訴你的代碼是工作在服務器端,以便:
<?php $abc = "<script>document.write(jvalue)</script>"?>
集$abc
到"<script>document.write(jvalue)</script>"
你寫就會有客戶端上運行的JavaScript,瀏覽器。不要混合使用它們...
好澄清所發生的事情:
本文是在一個PHP文件服務器調用,所以當:
Line Action on Server
<form name="form1"/> //->sent to client
<input type="text" name="code1" value="D50"//->sent to client
<input type="text" name="code2" value="" //->sent to client
<form/> //->sent to client
<script type="text/javascript"> //->sent to client
var jvalue = form1.code1.value; //->sent to client(will run on client)
<?php //->php takes control on server
$abc = "<script>document.write(jvalue)</script>"; //php runs this on server
//php sets $abc to "<script>document.write(jvalue)</script>" on server...
//php does not process javascript, php sees it as text...
?> //->ends php control
</script> //->sent to client
<?PHP //->php takes control on server again
$con = mysql_connect("localhost","abc_one","pass"); // php runs this on server
mysql_select_db("abc_one", $con); // php runs this on server
echo $abc; // php echoes $abc
含義,包含
線
<script>document.write(jvalue)</script>
發送給客戶端。
它被寫入到客戶端的文件...
它不會在服務器客戶端計算機上進行評估。
,並將在不久的將來,沒有爲什麼你看到瀏覽器D50發生....
?讓我們看看未來:
那麼當客戶端(瀏覽器)解析javascripts會發生什麼?
瀏覽器將執行
var jvalue = form1.code1.value;
線並設置jvalue到D50,當然當瀏覽器遇到
<script>document.write(jvalue)</script>
它將運行它,並顯示jvalue 的值上的客戶端這是D50 ..
所有這些都會發生在客戶端計算機上t在服務器上。
因此,這將在不久的將來,還沒有發生....
現在回到你的PHP文件
//$c = 'D50'; // lets ignore this
$c = $abc; // php runs this on server.
// guess what now $c is '<script>document.write(jvalue)</script>'
// then php tries to run this:
$result2 = mysql_query("SELECT * FROM tblmycode where code='$c';");
現在你的查詢變得非常有趣:
SELECT * FROM tblmycode where code='<script>document.write(jvalue)</script>';
我希望你現在明白髮生了什麼...
PS:mysql接口被棄用使用mysqli或P請請...
問題是...? – Shoe 2013-04-05 13:46:06
嘗試回顯$ abc的長度來診斷,有可能是空值或其他空格。如果是這樣修剪它... – Ihsan 2013-04-05 13:46:33
請指定什麼不工作或你卡在哪裏? – 2013-04-05 13:47:40