简体   繁体   中英

Update php variable and select option on page refresh

I have a php variable

$cur_selection='one';

which I currently have getting a default value of 'one' as shown above.

I have a drop down select called courses and a button which I would like by default to start off with the first Option.

<select id='courses'>
<option value='one'>Select Option</option>
<option value='two'>two</option>
<option value='three'>three</option>
</select>

<button onclick="RefreshpageAndChangeVariableandSelector()">Click Me!</button>

as you can see the list is populated with options that have values.

Upon selecting an option other than the default option then clicking the button I wish to have to page refresh updating the value of $cur_selection to be the value of the chosen option while also changing the selection in the courses drop down menu to what was chosen before clicking.

However I am clueless what to put into the RefreshpageAndChangeVariableandSelector() to do what I need it to do

For this solution I am not looking to use AJAX but to refresh to page

The only real answer using refresh (I can think of) involves query parameters. This will involve some pseudo code, but it should get you started. If you still have questions, comment and I can update my answer :)

Javascript

function RefreshpageAndChangeVariableandSelector(value)
{
    value = encodeURIComponent(value);
    window.location.href = window.location.href + '?selected=' + value;
    // Note, this needs more logic (in case the page is already ?selected=one)
}

PHP

$default_value = 'one';
$cur_selection = !empty($_GET['selected']) ? $_GET['selected'] : $default_value;

The Javascript function will take a value (or you can write logic to determine the value) and encode it with encodeURIComponent . Then it will append it as the query parameter selected . This part needs more logic to make sure we don't append a ? when its already there or send 2 different values for selected . If you are lost here, let me know and I'll help. By updating the window.location.href , Javascript will make a new GET request with the browser (not technically a refresh).

The PHP uses a shorthand conditional to make sure that $_GET['selected'] isn't empty() . This is necessary because you will get an exception if you just look for $_GET['selected'] when it isn't sent. If it is empty, we fallback to a default value.


Update

$default_value = 'one';

// This...
$cur_selection = !empty($_GET['selected']) ? $_GET['selected'] : $default_value;

// Is the same as...
if(!empty($_GET['selected'])) {
    $cur_selection = $_GET['selected'];
} else {
    $cur_selection = $default_value;
}

// And can also work like...
$cur_selection = $default_value;
if(!empty($_GET['selected'])) {
    $cur_selection = $_GET['selected'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM