Search code examples
phphtmlmysqltextbox

Get a value from a textbox in php, call another php file and pass this text box value as a variable to it


I have two php pages. They both return some MySQL tables.

I use page1.php to return the a table called "student" from the database. I am now trying to implement a search function that would help people search through the student table for a particular name.

I have created a textbox for the search ( This is in page1.php)

<INPUT TYPE = "TEXT" VALUE ="search">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Search">

What I am trying to do is : When the user clicks on the button "search", it calls page2.php and passes the value from the textbox to a variable called "sname" in page2.php

In page2.php

I have :

$sname = "matt"; /*<-- Need dynamic */
$result = mysql_query("Select * from student where phone like '%$sname%'");

Now I am manually feeding the value of $sname. How can I get the value from the textbox in page1.php to this variable?


Solution

  • set the code in page1.php like:

    <form action='page2.php' method='post'>
    <INPUT TYPE = "TEXT" NAME='search' VALUE ="search">
    <INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Search">
    </form>
    

    now in page 2 you can use $_POST['search'], like:

    $sname = $_POST['seach'];