I have a names database with only 4 fields (Sex, Origin, Meaning, Starts With) and html search form for those same 4 fields. Can anyone give me the php code so that if anyone fills in any or all of the form fields, the results from the database will be produced on the same page as the search form.

Recommended Answers

All 15 Replies

Hi, you can have php code

<?php echo $_SERVER['PHP_SELF'];?>

on your form action to return the results in the same page.

and in the top of your page(the same page with the form), add conditional statements for all/any or search form fields and then query them ..

Show us your code for us to debug it.
:)

hi and thanks for your reply. I have no code (embarrassed) and was hoping someone could give me example with only 4 fields to query. I have no idea about php. I know how to make html form and i have .sql file to import to database but that is all. I dont know how to write the php code to make it all happen. I am making searches for scripts to play with but they are very confusing for me. I would very much appreciate any help you can give.
kind regards

with that... try to search tutorials on google.. try website like w3schools.com, tizag.com.. there are many examples there.. including php and mysql for your data fetching.. when your done, post it quick here for us to know..

Actually its simple.. but we want something even a little effort.. that would really help you.

<form action='<?php echo $_SERVER['PHP_Self']; ?>'>
<input type='text' name='field1' value='<?php if(isset($field1)){echo $field1; } ?>'>
<input type='text' name='field2' value='<?php if(isset($field2)){echo $field2; } ?>'>
<input type='text' name='field3' value='<?php if(isset($field3)){echo $field3; } ?>'>
<input type='text' name='field4' value='<?php if(isset($field4)){echo $field4; } ?>'>
<input type='submit' onclick="return confirm('Are all fields complete?')">

I like to add confirmation to the submit button, degrades ok with javascript disabled,

Hi and thanks
I will try this too and let you know how it goes
regards

Logial Correction, and statement of the obvious :icon_confused:

<?php 
/* 
logical construct depends on your data,,,
validate data entries, 
if valid 
 insert to mysql, 
 disable submit so data will not be aded to table twice, 
 or redirect to another page 
else
 echo feedback to user to explain what is right/wrong with the submitted data  before the form is resubmitted
*/  ?>
<form action='<?php iecho $_SERVER['PHP_Self']; ?>'>
<input type='text' name='field1' value='<?php if(isset($field1)){echo $field1; } ?>'>
<input type='text' name='field2' value='<?php if(isset($field2)){echo $field2; } ?>'>
<input type='text' name='field3' value='<?php if(isset($field3)){echo $field3; } ?>'>
<input type='text' name='field4' value='<?php if(isset($field4)){echo $field4; } ?>'>
<input type='submit' onclick="return confirm('Are all fields complete?')">

Well now my go.

I have done something very very similar.

No you cannot have the code. It is MUCH MORE FUN it you work it out for your self.

For a start something that the previous posters forgot, what happens if more than one matching row is found (i.e. only the 'Sex' field was filled in?

The previous examples show HTML forms being used. I would suggest that you look at text boxes and handle the input / display using javascript and good old (new?) AJAX. Remember fields do not have to exist when the page is first displayed on the client screen. the DOM allows you to create objects, and set their values and all other properties on the fly.

QUICK TIP use different text areas for user input and data display, when the user submits some data expect it to be partial data and leave the focus in the same fiedl that the user last used. If the user submits "Pat" and you list out all of the Pat's / Patricia's & Partick's that you can find, the user will find it much easier to go on to finish Patraig.

I would recommend you get 'THE JAVASCRIPT BIBLE'. It contains a huge amount of information and will guide you through your first steps in implementing this type of function.

Hi thanks for your reply but you may as well have written it in arabic for all my understanding lol.

Allow me to explain - i'm absolutely new to php - had never even seen it before now beginning to research - i'm completely self taught html and some database and so what you have said makes no sense to me whatsoever. me being blonde doesn't help either lol so i wanna keep it very simple. I'm sure it would be a lot of fun to work it out myself but i work full time (not in IT) and have three kids so thats gonna take quite a while. I think my best bet is to just work through tutorial after tutorial, however long it takes - unless you wanna sell me your code lol - kidding

Thank you all for trying to help but i fear its just way over my head for now and maybe i should find another less confusing project to take up my 'spare' time (like naughts and crosses lol) - unfortunately i just don't 'get it'

.. hey phpNewbie.. umm the sites i gave was a great help.. i am also a self taught coder.. i only search , read forums.. and it gave so much more than i think of..

it's easy to paste the code here but you have to learn it a step in a time..

goodluck, willing to give u code that can help :)

enim213 - thank you very much for the links you supplied - i really do appreciate your time to give them and will definitely make an effort to get the understanding - be afraid lol - if i come up with anything i'll be right back here for you to debug :) (or get your pasted code - kidding - or not lol)
thank you almostbob for your link and code too
at least today i got my basic html page made and my database loaded so that's a start :)

Hi... i'm baaack
Ok.. i made some effort (got myself more confused in the process - seem to be so many ways to do the same thing??)

Anyway.. this is what i have now: Please help to add or change code to get this working. my sql statement works in phpmyadmin bringing back required results from the database but how do i get that happening on my webpage with the php and html.

Apologies for my messy code but like i explained before, i'm absolutely new to php and finding it very difficult to get my 'blonde' head around it.

Thanks again in advance for any help.

<html>
<head>
<title>Baby Names Database</title>
</head>
<body>
<h1>Baby Names</h1>
<h3>Please fill in the blanks below, and We'll return baby names for you to browse.</h3>

<form method = "post" action = "practiceBaby5.php">
<table border = "1">
<tr>
<th align = "left">Gender:</th>
<th>
<input type = "radio" name = "male" value = "yes">Boys Names<br />
<input type = "radio" name = "female" value = "yes">Girls Names<br />
</th>

<tr>
	<th align = "left">Meaning:</th>
	<th>
		<input type = "text" name = "meaning" value = "">
	</th>
</tr>

<tr>
	<th align = "left">Name Begins With:</th>
	<th>
		<input type = "text" name = "name" value = "">
	</th>
</tr>


<tr>
	<th align = "left">Origin</th>
	<th>
		<select name = "origin">
		<option value = "Muslim">Muslim</option>
		<option value = "anglo">Anglo</option>
		<option value = "african">African</option>
		<option value = "celtic">Celtic</option>
		</select>
	</th>
</tr>

<tr>
	<td colspan = 2> <br />
		<center>
			<input type = "submit" value = "Show me Names">
		</center>
	</td>
</tr>
</table>
</form>
		
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<?
// MySQL Connection Information
$server = "localhost";
$username = "sharlene";
$password = "";
$dbname = "baby_names";

// MySQL Connect String
$connection = mysql_connect($server,$username,$password);

// Select the database
mysql_select_db($dbname);

$gender = $_POST['gender'];
$meaning = $_POST['meaning'];
$name = $_POST['name'];
$origin = $_POST['origin'];

$gender = mysql_real_escape_string($gender);
$meaning = mysql_real_escape_string($meaning);
$name = mysql_real_escape_string($name);
$origin = mysql_real_escape_string($origin);

$sql = "SELECT * FROM names WHERE gender = '" . $gender . "' and name LIKE '" . $name . "%' and 

meaning LIKE '%" . $meaning . "%' and origin = '" . $origin . "'";
//print($sql);

//execute SQL query and get result
$sql_result = mysql_query($sql, $connection)
	or die(mysql_error());
//print("after sql");

// Loop through the data set and extract each row into it's own variable set
while ($row = mysql_fetch_array($sql_result)) {
	extract($row);
}
// Loop through the data set and extract each row into it's own variable set
//while ($row = mysql_fetch_array($sql_result)) {
//	echo $row['gender']." ".$row['meaning']." ".$row['name']." ".$row['origin']."<br>";

?>


</BODY>
</HTML>

hi i am working in search module i never worked before in search module,Please do some Help in search Module for better understanding "php code to search and show results on same page"

<script type="text/javascript" src="scripts/script.js"></script>
<table align="center" width="80%">

<?php
//session_start();

function basic()
{
    if(!isset($_SESSION['user']))
    $where = "where restriction = 'Public' and ";
    else
    $where = "where ";

    $searchbystring = mysql_real_escape_string($_POST['searchbystring']);


        $searchby = mysql_real_escape_string($_POST['searchby']);

        $nor = $_POST['nor'];

        if($nor == "10 Records")
        $nor = " limit 0, 10";
        else if($nor == "20 Records")
        $nor = " limit 0, 20";
        else if($nor == "50 Records")
        $nor = " limit 0, 50";
        else
        $nor = "";

        $field = checkfield($searchby);

        $querystring = $field . " like '%" . $searchbystring . "%'";


        $query = mysql_query("select distinct a.id, a.title_statement, a.pub_dist_etc, c.entry from record_table a left join subject_added_entry b on a.id = b.id left join main_entry c on a.id = c.id $where" . $querystring . $nor);

        $i = 1;

        if(mysql_num_rows($query) != 0)
        {       
            echo "<tr><th colspan='2'><h2 class='header'>Results</h2></th></tr>";

            while($results = mysql_fetch_array($query))
            {
                if($i == 1)
                {
                    echo "<tr><td><u><a class='collections' href='viewpublic.php?id=". $results['id'] ."'>" . $results['title_statement'] . "</a></u><br/>". $results['entry'] ."<br/>". $results['pub_dist_etc'] . "</td>";
                    $i = 2;
                }
                else
                {
                    echo "<td><u><a class='collections' href='viewpublic.php?id=". $results['id'] ."'>" . $results['title_statement'] . "</a></u><br/>". $results['entry'] ."<br/>". $results['pub_dist_etc'] . "</td></tr>";
                    $i = 1;
                }
            }

        }
        else
        echo "<tr><td><center>No Results</center></td></tr>";


}

function advance()
{   
    if(!isset($_SESSION['user']))
    $where = "where restriction = 'Public' and ";
    else
    $where = "where ";

    if(trim($_POST['searchbystring1']) != "")
    {
        $searchbystring = mysql_real_escape_string($_POST['searchbystring1']);
        $searchby = $_POST['searchby1'];

        $field = checkfield($searchby);

        $querystring = $field . " like '%" . $searchbystring . "%'";


    }

    if(trim($_POST['searchbystring1']) != "" && trim($_POST['searchbystring2']) != "")
    {
        $searchbystring = mysql_real_escape_string($_POST['searchbystring2']);
        $searchby = $_POST['searchby2'];
        $operator = $_POST['operator1'];

        $field = checkfield($searchby);

        $querystring .= " " . $operator . " " . $field . " like '%" . $searchbystring . "%'";


    }

    if(trim($_POST['searchbystring1']) != "" && trim($_POST['searchbystring2']) != "" && trim($_POST['searchbystring3']) != "")
    {
        $searchbystring = mysql_real_escape_string($_POST['searchbystring3']);
        $searchby = $_POST['searchby3'];
        $operator = $_POST['operator2'];

        $field = checkfield($searchby);

        $querystring .= " " . $operator . " " . $field . " like '%" . $searchbystring . "%'";


    }

    if(trim($_POST['searchbystring1']) != "")
    {
        $query = mysql_query("select distinct a.id, a.title_statement, a.pub_dist_etc, c.entry from record_table a left join subject_added_entry b on a.id = b.id left join main_entry c on a.id = c.id $where " . $querystring);

        $i = 1;

        if(mysql_num_rows($query) != 0)
        {       
            echo "<tr><th colspan='2'><h2 class='header'>Results</h2></th></tr>";



            while($results = mysql_fetch_array($query))
            {
                if($i == 1)
                {
                    echo "<tr><td><u><a class='collections' href='viewpublic.php?id=". $results['id'] ."'>" . $results['title_statement'] . "</a></u><br/>". $results['entry'] ."<br/>". $results['pub_dist_etc'] . "</td>";
                    $i = 2;
                }
                else
                {
                    echo "<td><u><a class='collections' href='viewpublic.php?id=". $results['id'] ."'>" . $results['title_statement'] . "</a></u><br/>". $results['entry'] ."<br/>". $results['pub_dist_etc'] . "</td></tr>";
                    $i = 1;
                }
            }

        }
        else
        echo "<tr><td><center>No Results</center></td></tr>";
    }
    else
    {
        echo "<center>First keyword must be filled up.</center>";
    }
}

function checkfield($field)
{
    if($field == "author")
    return "c.entry";

    if($field == "subject")
    return "b.entry";

    else
    return "a." . $field;
}

if(isset($_POST['search']))
basic();
if(isset($_POST['searchadv']))
advance();
?>


</table>

Bold Text Here

can someone help me to solve where could i put my page.php . and where the next page will be having a result , because everytime i click the next page it won't work.. the results was lost. can somebody help me for pagination with search engine ?

Why do you not post a new thread?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.