Jump to content

Return variable to a specific function


deslyxia

Recommended Posts

I have a form on my web page. When a user clicks the submit button it executes an external php file. The job of this file is to connect to my db and query a table based on user inputs. All of this is working fine so far. What i need to figure out is how to take the result of that query (which could be multiple records from my table) back into a table the user can see on the original web page.

 

The way i see it is i need to somehow pass the query result back to the main page and then ru na loop and dump everything into a table.

 

My form looks like this

 

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

 

External 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());

 

 

Code to build and fill my data table with the records.


//Display Results
echo "<table alingn=\"center\" width=\"400px;\">";
		echo "<tr>
				  <td>ID</td>
				  <td>FirstName</td>
				  <td>LastName</td>
			  </tr>";
	   	while ($row = mysql_fetch_assoc($result)){
    		echo "<tr>
    		          <td>".$row['ID']."</td>
        		      <td>".$row['FName']."</td>
        		      <td>".$row['LName']."</td>
       		      </tr>";
   			}

   			echo "</table>";   
   
?>

 

How do i get my query result from the external PHP file into the main html doc ... and build this table AFTER the form data is submitted?

Link to comment
https://forums.phpfreaks.com/topic/263612-return-variable-to-a-specific-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.