2013-09-21 38 views
0

嘗試編輯我在W3學校中找到的腳本。使用Javascript的PHP表單

編輯: 我想要一個搜索文本框。我可以在其中輸入數據,它會觸發PHP腳本並返回值,而無需重新加載主頁。因此,當我輸入「404040」並單擊搜索按鈕時,它應該執行adminsearchvip.php?q = 404040 然後返回值而不重新編碼主頁。 編輯結束。

原始腳本是一個下拉菜單,但我想要一個textboxt insted。

我不確定發送「提交」按鈕的代碼是否正確。

第一個文件searchvip.php

<html> 
<head> 
<script> 
function showUser() 
{ 
if (str=="") 
    { 
    document.getElementById("txtHint").innerHTML=""; 
    return; 
    } 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","adminsearchvip.php?q="+str,true); 
xmlhttp.send(); 
} 
</script> 
</head> 
<body> 

<form> 
<label for="Number"></label> 
<input type="text" name="Number" id="Numbers" value=""> 
<input type='button' onclick='showUser(Numbers.value)' value='Search' /> 
</form> 
<br> 
<div id="txtHint"><b>Person info will be listed here.</b></div> 

</body> 
</html> 

和腳本它自我。

adminsearchvip.php

<?php 
$q = intval($_GET['q']); 

$con = mysqli_connect('localhost','xxxxxx','xxxxx','jossan'); 
if (!$con) 
    { 
    die('Could not connect: ' . mysqli_error($con)); 
    } 

mysqli_select_db($con,"jossan"); 
$sql="SELECT * FROM vip_stat WHERE code = '".$q."'"; 

$result = mysqli_query($con,$sql); 

echo "<table border='1'> 
<tr> 
<th>Number</th> 
<th>Bought Date</th> 
<th>Type</th> 
<th>Price</th> 
<th>Used</th> 
<th>Activation Date</th> 
<th>Username</th> 
</tr>"; 

while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['number'] . "</td>"; 
    echo "<td>" . $row['time'] . "</td>"; 
    echo "<td>" . $row['type'] . "</td>"; 
    echo "<td>" . $row['Price'] . "</td>"; 
    echo "<td>" . $row['used'] . "</td>"; 
    echo "<td>" . $row['utime'] . "</td>"; 
    echo "<td>" . $row['username'] . "</td>"; 
    echo "</tr>"; 
    } 
echo "</table>"; 

mysqli_close($con); 
?> 

我已經測試了,還有我的網頁瀏覽器的www.homepage.com/adminsearchvip.php?q=9494943和工作,我得到了正確的respons。所以腳本似乎工作。但搜索頁面甚至沒有顯示任何內容。

+8

如果「魔獸學堂」你主持我知道意思是w3schools,你真的不應該讀他們發表的任何東西。要找出原因,請查看http://www.w3fools.com。 – rid

回答

0

從你的問題,我會建議你給jquery一試。

鏈接到jQuery的在你的頁面的頭部分,

showUser() 
{ 
var url='adminsearchvip.php'; 
var q = $("#Numbers").val(); 
$("#txtHint").html('Loading Please Wait...'); 
$.get(url,{ q: q } ,function(data){ $("#txtHint").html(data) }); 
} 

這應該爲你做它。

由於它的搜索,我使用$.get如果其發佈請求,使用$.post。讓你找到更多的問題

這裏是jQuery的鏈接通過谷歌CDN //ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js

這是你的形式看起來應該像

<form> 
<label for="Number"></label> 
<input type="text" name="Number" id="Numbers" value=""> 
<input type='button' onclick='javascript: showUser();' value='Search' /> 
</form> 
<br> 
<div id="txtHint"><b>Person info will be listed here.</b></div> 
+0

你好,非常感謝你的努力,我現在已經有一段時間了。閱讀一些jquery常見問題。無法讓它工作。 按鈕窗體應該如何顯示?

<標籤= 「號碼」>

+0

@ZaraRebeckaElisabethZentio我已經更新了我的答案,看看html表單。 – jcobhams

+0

謝謝你,我還挺解決了,現在我只需要在顯示部分工作:)謝謝你投入時間在這 –

0

確實更改以下行

<input type='button' onclick='return showUser()' value='Search' /> 

現在加入一號線在你的函數showUser。

function showUser() 
{ 
    var str = document.getElementById("Number").value; 

現在讓我知道你是否仍然有問題。 「

+0

感謝您的重播,它仍然無法正常工作。有什麼方法可以添加一些文本到div,所以我可以調試它真的發送了什麼? –

1

」試圖編輯我在W3學校找到的腳本。「

嗯,這是你的問題。 Don't use W3 schools

由於您想使用ajax加載html表格,並在用戶單擊按鈕時將其插入到您的dom中,您可以使用jquery load輕鬆完成此任務。下面是一個粗略的例子:

//note: put id="buttonId" on the button element in order for this to work 
$('#buttonId').click(function(){ 
    var value = $('#Numbers').val(); 
    if (value !== '') { 
     $('#txtHint').load("adminsearchvip.php?q="+value); 
    } 
}); 

順便說一句,你應該在你的adminsearchvip腳本中使用準備好的語句,而不是$ Q變量拼接到SQL的。

+0

我試圖做的是有一個JavaScript來顯示來自mysql表的信息,無需重新加載。 –

+0

+1 Rob Apodaca爲jQuery的見解。嘗試使用jQuery來完成您的請求。這會容易得多。 – jcobhams

+0

謝謝您的重播。我對這個東西並不擅長,但是我很新,但是儘量不要只爲讀書而努力學習。我看了一些關於表單等的Jquery主頁,但我真的很明白這一切,因爲我找不到任何與MYSQL示例。你可以給我一個片段或東西來玩弄使用JQuery和MySQL? –