Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a transient from a select option in PHP

I'm trying to set a transient (API reference) from a select option. If I set the transient without redirecting to another page, just set it to redirect to itself, then the transient is set, and when I manually input the URL of the page I should redirect to, it shows up.

However if I directly redirect, it doesn't register that I've set the transient. I've come to the conclusion that how I get the variable from the select element is the problem, since it works just like it should if I just put a normal string as the transient, e.g. (on page one):

set_transient('transient_test', "this is a test", 60 * 30);

Here's how I get the the variable from the select element (on page one):

if (isset($_POST['industry']) == true) {
      $selectOption = $_POST['industry'];
      set_transient('industry_transient', $selectOption, 60 * 30);
}

I test if the transient is the variable I want it to be (on page one):

if (isset($_POST['industry']) == true) {
                                $selectOption = $_POST['industry'];
                                set_transient('industry_transient', $selectOption, 60 * 30);
                                $selected = get_transient('industry_transient');
                                echo $selected;
                        }

Here's how I get the transient (on page 2):

$transient = get_transient('industry_transient');

I test it like this, and I always get a "no transient" when I redirect immediately (on page 2):

if (false === ($value = get_transient('industry_transient'))) {
      echo 'no transient';
}

if (true === ($value = get_transient('industry_transient'))) {
      echo 'yes transient';
}

Here's my form (page one):

<form method="post" action="http://localhost:8888/wordpress/candidates/industry">
                        <select name="industry">
                                <option disabled selected> -- Select an industry -- </option>
                                        <option value="Example">there are a lot of options...</option>
                        </select>
                        <input type="submit" value="Submit">
                </form>

I tried setting the transient within the form tag, however, it made no difference.

like image 431
Rasmus Avatar asked Nov 20 '25 09:11

Rasmus


1 Answers

Based on the locations of the files, it looks like you just need to move the code that sets the transient to page two.

Where ever the form points to is going to be the location of the $_POST data. Since you're pointing the form to page 2, you should have this code on page two.

if (isset($_POST['industry']) == true) {
    $selectOption = $_POST['industry'];
    set_transient('industry_transient', $selectOption, 60 * 30);
}

It should of course be above other code. At this point, it's not really necessary to run this:

$transient = get_transient('industry_transient');

unless you're looking to double check that it worked. It would be a little more efficient to simply use the $selectOption you already set, above.

like image 74
Jacob Raccuia Avatar answered Nov 21 '25 23:11

Jacob Raccuia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!