在NetBeans中运行AJAX PHP示例

问题描述:

我发现什么看起来像使用PHP和MYSQL的AJAX的体面入门教程,并将它跟在字母上 - 这是我第一次尝试AJAX和PHP,我希望在NetBeans中运行它,但不要不知道如何让它运行。我如何在NetBeans中运行?我已经将主文件设置为ajax.html并尝试使用绿色箭头运行,但是当出现HTML页面时,当我输入有效数据并单击“查询MySQL”按钮时,它什么都不做。在NetBeans中运行AJAX PHP示例

这里是我们的ajax.html文件

<html> 
<body> 
<script language="javascript" type="text/javascript"> 
<!-- 
// Browser support code 
function ajaxFunction(){ 
var ajaxRequest; // the variable that makes AJAX possible 
try{ 
    // Opera 8.0+, Firefox, Safari 
    ajaxRequest = new XMLHttpRequest(); 
} catch(e){ 
// internet explorer browsers 
    try{ 
    ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
    }catch(e){ 
    try{ 
     ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
     } catch(e) { 
     // something wrong in creating XMLHttpRequest 
     alert("Browser didn't create XMLHttpRequest"); 
     return false; 
     } 
    } 
} 

// Now get the value from user and pass it to 
// server script 
var age = document.getElementById('age').value; 
var wpm = document.getElementById('wpm').value; 
var sex = document.getElementById('sex').value; 

var queryString = "?age=" + age; 
queryString += "&wpm=" + wpm +"&sex=" + sex; 
ajaxRequest.open("GET", "ajax-example.php" + queryString, true); 
ajaxRequest.send(null); 
} 
// --> 
</script> 

<form name='myForm'> 
<br /> 
    Max Age: <input type='text' id='age' /> <br /> 
<br /> 
    Max WPM: <input type='text' id='wpm' /> 
<br /> 
<br /> 
Sex: <select id='sex'> 
<option value="m">m</option> 
<option value="f">f</option> 
</select> 
<input type='button' onClick='ajaxFunction()' value='Query MySQL' /> 
</form> 
<div id='ajaxDiv'>Your result will be displayed here</div> 
</body> 
</html> 

这是ajax-example.php文件。我的机器上的MySQL似乎是“localhost3306”和使用的端口号7777

<?php 

$dbhost = "localhost3306:7777"; 
$dbuser = "root"; 
$dbpass = "password"; 
$dbname = "web_prof_tracker"; 

// Connect to MySQL server 
mysql_connect($dbhost, $dbuser, $dbpass); 

// Select database 
mysql_select_db($dbname) or die(mysql_error); 

// Retrieve the data from Query String 
$age = $_GET['age']; 
$sex = $_GET['sex']; 
$wpm = $_GET['wpm']; 

// Escape user input to help prevent SQL injection 
$age = mysql_real_escape_string($age); 
$sex = mysql_real_escape_string($sex); 
$wpm = mysql_real_escape_string($wpm); 

// Build query 
$query = "SELECT * FROM ajax_example WHERE sex = '$sex'"; 
if(is_numeric($age)) 
    $query .= " AND age <= $age"; 
if(is_numeric($wpm)) 
    $query .= " AND wpm <= $wpm"; 

// Execute query 
$qry_result = mysql_query($query) or die (mysql_error()); 

// Build result string 
$display_string = "<table>"; 
$display_string .= "<tr>"; 
$display_string .= "<th>Name</th>"; 
$display_string .= "<th>Age</th>"; 
$display_string .= "<th>Sex</th>"; 
$display_string .= "<th>WPM</th>"; 
$display_string .= "</tr>"; 

// Insert a new row in the table for each person returned 
while($row = mysql_fetch_array($sql_result)){ 
$display_string = "<tr>"; 
$display_string .= "<td>$row[name]</td>"; 
$display_string .= "<td>$row[age]</td>"; 
$display_string .= "<td>$row[sex]</td>"; 
$display_string .= "<td>$row[wpm]</td>"; 
$display_string .= "</tr>"; 
} 
echo "Query: " . $query . "<br />"; 

$display_string .= "</table>"; 
echo $display_string; 
?> 
+0

如果你刚开始学习,建议你停在那儿,发现不使用折旧'mysql_ *'也使教程确保本教程确实处理SQL注入预防 –

+0

您需要有运行PHP的Web服务器。 – webDev

+0

@webDev Netbeans通常会与tomcat一起... –

在正式净豆网站netbeans.org他们展示如何使用NetBeans

这是运行PHP项目的教程步骤

我将跳过php环境的配置,因为您已经提到您拥有apache并且您曾经运行过php项目。

在您的NetBeans这是你需要做什么:

  1. 启动IDE,切换到项目窗口,选择File> New 项目。 Choose Project面板打开。
  2. 在类别列表中,选择PHP。
  3. 在Projects区域中,选择PHP Application,然后单击Next。新建 PHP项目>名称和位置面板打开。

enter image description here

确保您的源文件夹里面的htdocs在你的XAMPP。

然后点击下一步,选择Run As本地网站,那么指定项目的URL

enter image description here

然后你做你的安装配置

你需要再打开项目文件,那么你的时候DONE从菜单中选择运行

enter image description here

ŧ如果您的项目将显示在您的默认NetBeans项目浏览器上。

希望这会有所帮助,祝你好运

来源:https://netbeans.org/kb/docs/php/quickstart.html