0

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

7
  • Am I correct in understand that you want the form action to be dynamic based on the form inputs? Commented Mar 14, 2016 at 22:50
  • This is can't be done alone with PHP, you need to use AJAX (client-side) request to your PHP that sends the param and then gets the response. Commented Mar 14, 2016 at 22:50
  • This sounds like an XY Problem Commented Mar 14, 2016 at 22:52
  • @amflare I tried something dynamic with jQuery but it didn't trigger the function , it just wrote the call into url Commented Mar 14, 2016 at 22:54
  • @Akam is AJAX the only way ? I'll have to read more doc so, just thought that I was missing something Commented Mar 14, 2016 at 22:55

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.