【发布时间】:2017-08-28 13:50:16
【问题描述】:
我找到了一个看起来不错的关于使用 PHP 和 MYSQL 进行 AJAX 的介绍性教程,并一字不差地遵循了它——这是我第一次尝试 AJAX 和 PHP,我希望在 NetBeans 中运行它,但不知道如何获得它运行。如何在 NetBeans 中运行它?我已将主文件设置为 ajax.html 并尝试使用绿色箭头运行,但是当 HTML 页面出现时,当我输入有效数据并单击“查询 MySQL”按钮时它什么也不做
这里是 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;
?>
【问题讨论】:
-
如果你刚刚开始学习,建议你停在那里,找一个不使用已贬值的
mysql_*的教程,同时确保该教程确实处理了sql注入预防 -
你需要有web服务器来运行php。
-
@webDev Netbeans 通常会提供 tomcat...
-
Tomcat 适用于 Java Web 应用程序和 PHP,您需要安装 Apache。我认为需要一些桥接才能在tomcat上同时使用php/java。如果 tomcat 8 那么我该如何运行 php @MasivuyeCokile,我想知道你是否有一些链接可以查看。
-
其实有一篇关于如何在apache上运行php的帖子:stackoverflow.com/questions/779246/run-a-php-app-using-tomcat
标签: javascript php mysql ajax