raziel2101
raziel2101

Reputation: 13

php - send variable to next page by link

I need help.

How can I call the variable(within the url) after a successful search result in my index.php to the next page view.php so that I can view the complete data of the variable?

heres my code

"index.php"

<form method="post" action="index.php?go" id="searchform">
<input type="text" name="name" size = "50">
<br/>
<input type="submit" name="submit" value="SEARCH">
<button type="reset" value="Reset">RESET</button>
</form>


<?php

    $db=mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error()); 
    $mydb=mysql_select_db("emp_dbA");

    if(isset($_POST['submit'])){
    if(isset($_GET['go'])){
    if(preg_match("/[A-Z | a-z]+/", $_POST['name'])){
    $name=$_POST['name'];
    $letter=$_GET['by'];

    $sql="SELECT emp_ID, fname, lname,mname FROM emp_tbl WHERE fname LIKE '%" . $name . "%' OR lname LIKE '%" . $name ."%' OR mname LIKE '%" .$name . "%'";
    $result=mysql_query($sql);  
    $numrows=mysql_num_rows($result);

    echo "<p>" .$numrows . " results found for " . stripslashes($name) . "</p>"; 

    while($row=mysql_fetch_array($result)){

    $fname =$row['fname'];
    $mname =$row['mname'];
    $lname=$row['lname'];
    $ID=$row['emp_ID'];

        echo "<ul>\n"; 
        echo "<li>" . "<a href=\"view.php?id=$fname\">"  .$fname . " " . $mname. " " . $lname . "</a></li>\n";
        echo  "</ul>";
                                            }
                                                    }
        else{
        echo "<p>Please enter a search query</p>";
            }


                            }
    }

?>

Upvotes: 0

Views: 273

Answers (5)

You get the URL parameters with the $_GET environment variable, while the field you did put inside the form (given that you chose post as the form method) will be read like $_POST["name"].

As a little piece of advice, try to resort to the GET method as few times as possible. Parameters received with the GET method are seen in the URL, while parameters received from the POST method aren't.

And, of course, sanitize all your GET/POST data before actually using it.

Upvotes: 0

user2294763
user2294763

Reputation: 3

Why not use a pagination class? Look this: http://phpsnaps.com/snaps/view/php-pagination-class/ PS. mysql is deprecated, use mysqli.

Upvotes: 0

silentw
silentw

Reputation: 4885

By using the reserved variable $_GET

$_GET['id']

REFERENCE

Upvotes: 1

slynx
slynx

Reputation: 21

$_SERVER['PHP_SELF'] might be a good use here if you want to handle this data on your index.php page. If you'd like to pass it to view.php, action should be:

<form action="view.php" method="POST"></form>

Upvotes: 0

Glitch Desire
Glitch Desire

Reputation: 15023

You can use GET or POST to send data to the next page.

http://www.tizag.com/phpT/postget.php

Upvotes: 1

Related Questions