0

I have a sign up form on my homepage and would like to capture the user's entries and have them populated into the customer registration form on a new page.

Is there a way to do this in WordPress?

2
  • 2
    Hey @Urick that's a great question. However it's not one you can solve with a click of a button. You can pass the variables from URL and GET them in the registration form on the new page. That is not the method if you are passing passwords though. In that case maybe you have to save them in a superglobal, like session. Mar 26, 2021 at 9:10
  • 3
    What have you tried so far? Can we see the code you've written? Mar 26, 2021 at 10:01

1 Answer 1

0

While this is not a WordPress specific problem it is not a non-WordPress problem either. It sounds like you need a little guidance on how forms pass data.

If what you are really asking is how to get a plugin to pre-populate a form, you are best off asking the plugin's developer.

Assuming that you want to do this yourself, here is a primer on things you will need to know.

There are two (that we are going to talk about) ways for a form to send data. These are the GET request and the POST request.

Let's start with a field, we shall call it foo.

<form  method="get">
    <label for="foo">Foo</label><input type="text" value="" name="foo" />
    <input type="submit" value="submit">
</form>

You will notice that I set the method to get. That means that the form data (foo) will be added to the end of the URL. It might look like this if the user typed "bar":

https://example.com/myform.php?foo=bar

Your users' data will be found in $_GET['foo']. However, you need to check if it exists.

if(isset($_GET['foo'])){
    $foo = $_GET['foo']; // or whatever
}

The advantage of GET is that users can share links with the data in them. The disadvantage is that users can share links with the data in them. (It depends on what you need to do with it).

The other way is to use POST. Like this:

<form  method="post">
    <label for="foo">Foo</label><input type="text" value="" name="foo" />
    <input type="submit" value="submit">
</form>

Notice that the method in the form has changed. This page has a guide to form methods. Now the form data will be sent using the post method (not in the URL) and can be accessed via the POST global.

if(isset($_POST['foo'])){
    $foo = $_POST['foo']; // or whatever
}

So now you want to put this data in another form. Here is how you might do it.

<?php
// other code maybe
$foo = '';
if(isset($_POST['foo'])){
    $foo = $_POST['foo'];
}
?>
<form  method="post">
    <label for="foo">Foo</label><input type="text" value="<?php echo $foo; ?>" name="foo" />
    <input type="submit" value="submit">
</form>

In this last example, I made an empty var called $foo. If the user sent us a foo in the POST, I added the value to the new var. Then I spat it out in the form. This way the form is pre-filled with the foo we just got.

Note: This is not an inherently safe way to do things. A bad actor could send you bad code that could break your form or even your entire site. Take a look at WordPress string sanitisation functions.

For example:

if(isset($_POST['foo'])){
    $foo = sanitize_text_field($_POST['foo']);
}

That is much safer.

You should now have some understanding of how forms pass information. If you are creating your own forms, this could be useful to you.

At the very least, this should get you started. You can always come back with more specific questions if you get stuck a little further on.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.