Jump to content

Return php query result to webpage.


deslyxia

Recommended Posts

Good Afternoon,

 

The issue i have is as follows. I have a form on my webpage, WHen the user clicks the submit button it excecutes an external php file which querys my database. I need to get the results of that query back into my main web page in a table. In researching this It seems that i need to use AJAX and possibly JQuery to do this ... but i have never used either so im lost.

 

Any help with this would be a great help as well as a great learning tool.

 

<form id="searchForm" name="searchForm" method="post" action="psearch.php" autocomplete="off">
	  		<table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
   		  		    <tr>
   		    	  	  <td width="112"><b>First Name</b></td>
    			      <td width="188"><input name="fname" type="text" class="textfield" id="fname" /></td>
   					</tr>
	    		<tr>
   					  <td><b>Last Name</b></td>
      				  <td><input name="lname" type="text" class="textfield" id="lname" /></td>
    				</tr>
    				<tr>
      		   		  <td> </td>
      		   	      <td><input type="submit" name="Submit" value="Search" /></td>
    				</tr>
      		</table>
		</form>

 

PhP File

 

<?php
      //Start session
session_start();

//Include database connection details
require_once('config.php');

//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;
  
  //Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
	die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
	die("Unable to select database");
}

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
	$str = @trim($str);
	if(get_magic_quotes_gpc()) {
		$str = stripslashes($str);
	}
	return mysql_real_escape_string($str);
}

//Sanitize the POST values
$fname = clean($_POST['fname']);
$lname = clean($_POST['lname']);


//Query the Database
$query = "select * from Patient where FName like '%$fname%' and LName like '%$lname%' ";
$result = mysql_query($query) or die(mysql_error());

Link to comment
Share on other sites

AJAX is if you need to get show the data without loading an entire page, your current code appears to load the psearch.php page upon submit.

 

After your:

$result = mysql_query($query) or die(mysql_error());

Put:

$row = mysql_fetch_assoc($result);
echo $row['FName'];

 

This should give you an idea of how to do whatever you need to do.

Link to comment
Share on other sites

AJAX is if you need to get show the data without loading an entire page, your current code appears to load the psearch.php page upon submit.

 

psearch.php is just an external php file that i execute upon submit. It logs into the DB and performs a query. I somehow need to get my result set back onto the original page. How do i do this lol

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.