LaMut
LaMut

Reputation: 79

Php, form, redirect and queries

I feel like it's a common question but I didn't find a proper answer for it so I ask you ! ;)

Let's start with the form in classic index.php :

...
    <form action="<?php echo BASE_URL; ?>">
        <div style="color:white">Instructions</div>
        <input type="number" name="param" />
        <input type="hidden" name="action" value="view" />
        <input type="hidden" name="controller" value="Solution" />

        <input type="????" name="instance_id" value="?????" />

        <input type="submit" value="Solutions Listing" id="button_solutions">
    </form>
...

The question here is the last input field before the submit button. The user is supposed to enter param in the first non-hidden input, and that should trigger a php function which determine the instance_id by picking into a database.

What I want to do is that when the user click on submit, it redirect through a link like

BASE_URL."?controller=Solution&action=view&param=toto&instance_id=value"

let's say my function in php which pick into the database is called toto(), toto returns instance_id. I know how to point to an url with GET parameters via the form, but I don't know how to trigger the php function. I know how to determine instance_id via the param input but I don't know how to create a redirection with GET parameters via a php function

please any help would be considered Thanks

Upvotes: 0

Views: 67

Answers (1)

Philipp
Philipp

Reputation: 15639

If you don't need to see the instance_id on your client, your could create some kind of redirect page, which retrieves the instance_id from your database. This page could be the same as your destination page.

<form method="GET" action="<?php echo BASE_URL; ?>">
    <div style="color:white">Instructions</div>
    <input type="number" name="param" />
    <input type="hidden" name="action" value="view" />
    <input type="hidden" name="controller" value="Solution" />

    <input type="submit" value="Solutions Listing" id="button_solutions">
</form>

and in your php script

// instance_id not set
if (!isset($_GET['instance_id'])) {
    $args = $_GET;
    $param = $args['param'];

    // do something with param and save it to instance_id
    $instance_id = ...

    $args['instance_id'] = $instance_id;

    // create get query
    $query = http_build_query($args);

    $url = BASE_URL . '?' . $query;
    header('Location: ' . $url);
} else {
    // instance_id set - do something with it
    $instance_id = $_GET['instance_id'];
}

If you can't use this approach and have to show the result of your instance_id the same time, the user enters the param, you have to use an ajax request, which sends an request to the server to get the instance_id.

An JavaScript (using jQuery) to do this could look like this:

$('input[name="param"]').on('change', function() {
    $.ajax({
        url: '...',
        data: { param: $(this).val() },
        method: 'GET'
    }).done(function(data) {
        $('input[name="instance_id"]').val(data);
    });
});

The php file get the param as GET value and should return the instance_id from your database.

Upvotes: 1

Related Questions