b0w3rb0w3r
b0w3rb0w3r

Reputation: 937

Passing $_GET variables using form submit action

I am trying to pass the table and database variables into a new page via the url so I can dynamically query the specified database and table using $_GET. I have tried saving the variables and appending them to the page using window.onload with JavaScript but the error_log says Undefined index: db & tbl which I assume means the variables are not being passed correctly.

I am therefore asking if I can pass these variables using the submit form action instead of using JavaScript ?? Or how can I fix the script to properly pass in the variables?

My Form

    <form method="post" action="db_search.php">
        Search : <input type="text" name="name" id="name" autocomplete="off"
            value="<?php echo $val;?>"> <input type="submit" name="submit"
            id="submit" value="Search">
    </form>

Script in db_search.php

function pageSet () {
    var yourTbl = window.sessionStorage.getItem("tbl");
    var yourDB = window.sessionStorage.getItem("db");
    window.location.href ='db_search.php?db=' + yourDB + '&tbl=' + yourTbl ;
};
window.onload=pageSet();

When db_search.php loads, the variables are appended to the url but the page just keeps refreshing.

Upvotes: 0

Views: 2764

Answers (3)

mvbrakel
mvbrakel

Reputation: 936

You could adjust your PHP to use $_REQUEST and adjust the form as follows:

<form method="post" action="db_search.php">
    <input type="hidden" name="db" value="<?php echo $_REQUEST['db'] ?>" />
    <input type="hidden" name="tbl" value="<?php echo $_REQUEST['tbl'] ?>" />
    Search : <input type="text" name="name" id="name" autocomplete="off"
        value="<?php echo $val;?>"> <input type="submit" name="submit"
        id="submit" value="Search">
</form>

This will add the $_GET parameters as $_POST parameters. Your script will match $_REQUEST parameters, thus both $_POST and $_GET occurances of 'db' will be used. $_POST has priority over $_GET in $_REQUEST.

Making it concrete:

//You now match db like:
$db = $_GET['db'];

// Change that to
$db = $_REQUEST['db'];

This way no matter if db is sent through GET or POST, it will be filled

Upvotes: 2

Matt
Matt

Reputation: 394

You could use the $_POST variable.

Or perhaps you should think about storing those variables server side in a PHP Session.

<?php
    session_start();
    $_SESSION['tbl'] = $_POST['tbl'];
    $_SESSION['db'] = $_POST['db'];
?>

Then get them out agains

<?php
    session_start();
    $table = $_SESSION['tbl'];
    $db = $_SESSION['db'];
?>

Upvotes: 0

Ahmed Syed
Ahmed Syed

Reputation: 1189

try this;

<form method="get" action="db_search.php">

Upvotes: 0

Related Questions