0

I found what looks like a decent introductory tutorial on AJAX using PHP and MYSQL and followed it to the letter - this is my first attempt at AJAX and PHP and I wish to run this in NetBeans but don't know how to get it running. How do I make this run in NetBeans? I have set the main file to ajax.html and attempted to run using the Green arrow but when the HTML page appears it does nothing when I enter valid data and click the "Query MySQL" button

Here us the ajax.html file

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

Here is the ajax-example.php file. The MySQL on my machine appears to be "localhost3306" and using the port number 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;
?>
AJF
  • 1,801
  • 4
  • 27
  • 54
  • if you have just started learning, suggest you stop right there, and find a tutorial that does not use the depreciated `mysql_*` also make sure the tutorial does deal with sql injection prevention – Masivuye Cokile Apr 03 '17 at 15:55
  • You need to have web server for php running. – BetaDev Apr 03 '17 at 15:56
  • @webDev Netbeans usualy comes up with tomcat... – Masivuye Cokile Apr 03 '17 at 16:00
  • Tomcat is for Java web application and for PHP you nedd to install Apache. need some bridging to have working both php/java on tomcat i think. If tomcat 8 then how can I run php @MasivuyeCokile, i wanna learn if you have some link to look at. – BetaDev Apr 03 '17 at 16:05
  • Its better to install XAMPP on your machine and have your php application hosted on apache server (put all your code inside htdocs directory of xampp). Run your apache server. RUn your app using browser. – BetaDev Apr 03 '17 at 16:12
  • U are right @webDev my mind was on jsp, he will need to install apache then configure run environment on netbeans – Masivuye Cokile Apr 03 '17 at 16:26
  • 1
    Actually there's a post on how to run php on apache : http://stackoverflow.com/questions/779246/run-a-php-app-using-tomcat – Masivuye Cokile Apr 03 '17 at 16:29
  • @MasivuyeCokile Thanks for feedback. I should have mentioned I do have apache installed and running because I had done a few quick tutorials on PHP alone and had these running through Apache. But I will look at the link you posted and start learning this from a more basic tutorial too, Thank you – AJF Apr 04 '17 at 09:20
  • @webDev Thanks for your feedback. I should have mentioned I do have apache installed and running because I had done a few quick tutorials on PHP alone and had these running through Apache. I think I will start again at a more basic level. thank you – AJF Apr 04 '17 at 09:21

1 Answers1

2

On the officially net beans website netbeans.org they do show a tutorial on how to run php projects using netbeans

This are the steps,

I will skip the configuration of php environment as you have mentioned that you do have apache and you used to run php projects.

On your netbeans this is what you need to do :

  1. Start the IDE, switch to the Projects window, and choose File > New Project. The Choose Project panel opens.
  2. In the Categories list, choose PHP.
  3. In the Projects area, choose PHP Application and click Next. The New PHP Project > Name and Location panel opens.

enter image description here

Make sure your source folder is inside htdocs in your xampp.

Then click next and select Run As a local website then specify the project url

enter image description here

Then you have done your setup configuration

you need to then open the projects files then when you done choose run from the menu

enter image description here

Then your project will display on your default netbeans projects browser.

Hope this will help, Good luck

Source : https://netbeans.org/kb/docs/php/quickstart.html

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34