-4

I have a form on my homepage that takes in one input. After the form is submitted it redirects to a new Wordpress page test123 that gets its content from test-template.php

The content is unique for each person (generated from the users input).

So I am trying to find a way to assign a unique URL for the content as soon as the form is submitted, and then show the content on that URL.

Edit (extra details):

<form method='post' action='<?php bloginfo('url'); ?>/test123/' >
    Email: <input type="text" name="email" aria-required="true" required>
    <input type="submit">
</form>

After the form is submitted, the page reloads to example.com/test123, instead it should reload to example.com/RANDOM-ID

Lau
  • 1
  • 4
  • 3
    Yes, and? What have you tried? – tadman Feb 28 '17 at 19:25
  • Well I know how to generate the random sequence using either uniqueid() or md5(), but haven't tried adding it to the URL yet, as I have no idea where to start with it (I attempted generating the random URL via `action = "..."`), without any luck. – Lau Feb 28 '17 at 19:29
  • 1
    Don't use MD5. Please. Ever. For anything. Forget it even exists. Why not generate random identifiers? – tadman Feb 28 '17 at 19:31
  • Oh, thanks for the warning (my severe lack of PHP knowledge is probably showing). I appreciate the suggestion, but still have no idea how you push any random id to the url, because the `
    ` includes the page name `test123`, so it seems it's always going to redirect to that. I've seen and attempted some of the suggestions from http://stackoverflow.com/questions/5422065/php-random-url-names-short-url , also without any success.
    – Lau Feb 28 '17 at 19:37
  • Is this something you're trying to build into a WordPress application, or are you trying to integrate *with* an external WordPress app? – tadman Feb 28 '17 at 21:36
  • I am building it into a WordPress application. – Lau Feb 28 '17 at 23:21

1 Answers1

1

It is not easy to help you, since I don't know how your code looks like. But anyway, let's give it a try.

You can simply use uniqueid().

If you are working with a form, just use a hidden field like this:

<input type="hidden" name="unique_id" value="<?= uniqueid(); ?>">

Then in your PHP file just use echo $_POST['unique_id'];.

Another option would be using the unique ID directly in the URL, somehow like this:

example.com?id=<?= uniqueid(); ?>

In this case you get the value by using echo $_GET['id']; in your PHP file.

Update

This would be your form:

<form method='post' action='<?php bloginfo("url"); ?>/test123/' >
    Email: <input type="text" name="email" aria-required="true" required>
    <input type="hidden" name="hidden_id" value="<?php echo uniqid(); ?>">
    <input type="submit">
</form>

And this you can use for your PHP file to get the unique ID:

Unique ID: <?php echo $id = (isset($_POST['hidden_id'])) ? $_POST['hidden_id'] : 'No ID found.'; ?>
Reza Saadati
  • 1,224
  • 13
  • 27
  • Thanks for the suggestions. Unfortunately neither method worked. The first just prevents my homepage from loading entirely. The second won't allow me to add the unique ID directly in the URL on WordPress. I've updated the orignal question with the form code, in case that gives any ideas. – Lau Mar 01 '17 at 00:55
  • @Lau you have a syntax error on the first line of your code. I have just updated my answer and also tested the code. It works for me and I hope that it also works for you. – Reza Saadati Mar 01 '17 at 19:18