FreewayTalk

3 replies to this thread. Most Recent

Richard Cacciato

14 Jan 2021, 7:24 pm

Auto-enter form data

I want to create a form where data is automatically entered in a field from a url.

As an example, this url domain.com/orderform.html?order=A123

would load the form orderform.html and automatically fill in the order field with the value A123.

Is there an easy way to do this?

I read here https://stackoverflow.com/questions/46146803/auto-populate-input-form-field-from-url-parameters that I can do this if I use “$_GET” but haven’t been able to figure out how to do it. Would love to know how.

Thanks in advance.

waltd

15 Jan 2021, 2:01 pm

The $_GET reference would require PHP. If you’re interested in doing that, you could simply change the file extension of your page to .php, and then in the Value property of the form element named order, you would put <?php echo $_GET['order']; ?>. That will look awful in Freeway, but when viewed on a PHP server, it should just work.

If there isn’t a querystring with order= in it, this will just do nothing (it will generate a warning, but those are usually not shown on screen in production servers, so you get away with it). If you wanted to be more professional about it, you could do something like this in the Page / HTML Markup / Before HTML section:

<?php 
$order = '';
if(isset($_GET['order'])){
  $order = $_GET['order'];
}
?>

…then, back in your order field, replace the previous raw echo of the $_GET global with simply <?php echo $order; ?>.

For something as lightweight as this, though, you could also just use JavaScript, and not require anything special on the server. Again, if your field is named order, you would do something like this (this time in the Before /Body part of the Page / HTML Markup dialog):

    <script>
    var order = document.getElementById('order');
    var params = new URLSearchParams(window.location.search);
    order.value = params.get('order') || '';
    </script>

Stepping through that one line at a time, the first line gets a reference to the field, relying on it having the ID set to order. If you name a field order, Freeway will do the correct thing here, but if this doesn’t work, that’s probably the problem. You can inspect the HTML in your browser and see if you find `<input type=”text” id=”order” name=”order”> in there somewhere. If you don’t, then use the Inspector to ensure that the name of the field matches your expectations.

The second line gets an object reference to the querystring (formal name of ?foo=bar in a URL).

The third line sets the value of the field to either the order value from the querystring or an empty string if there is no order attribute in the URL.

Short and simple, for certain definitions of simple.

Walter

On Jan 14, 2021, at 2:24 PM, Richard Cacciato <[email protected]> wrote:

I want to create a form where data is automatically entered in a field from a url.

As an example, this url domain.com/orderform.html?order=A123

would load the form orderform.html and automatically fill in the order field with the value A123.

Is there an easy way to do this?

I read here https://stackoverflow.com/questions/46146803/auto-populate-input-form-field-from-url-parameters that I can do this if I use “$_GET” but haven’t been able to figure out how to do it. Would love to know how.

Thanks in advance.

Freeway user since 1997

http://www.walterdavisstudio.com

Richard Cacciato

15 Jan 2021, 8:06 pm

Thanks, Walter, the first example is what I was trying to do and it was super simple. Is there a way to prevent the user from modifying the value entered using this system?

Back to Top

waltd

15 Jan 2021, 11:47 pm

You could make the form element a hidden field, those are pretty hard to change unless you’re a developer. You can’t make it completely unchangeable, because they can just fiddle the URL and make it read whatever they want.

Walter

On Jan 15, 2021, at 3:06 PM, Richard Cacciato <[email protected]> wrote:

Thanks, Walter, the first example is what I was trying to do and it was super simple. Is there a way to prevent the user from modifying the value entered using this system?

Freeway user since 1997

http://www.walterdavisstudio.com