Jump to content

[SOLVED] Using OnChange with PHP


Recommended Posts

I'm trying to code a skin selector for my website, and I'm having trouble changing a cookie when selecting from a select form. Basically what I want to do is something thing like this:

 

<form>
<select onChange='<?php setcookie(MHQ_skin, selectedvaluehere, time()+(60*60*24*365)); ?>;window.location.reload();'>
<option value='3year'> 3rd Anniversary Skin
<option value='orig'> Original Skin
</select>
</form>

 

Is there anyway way that that's possible? Let me know if you need more info.

 

(I know that code won't work.)

Link to comment
https://forums.phpfreaks.com/topic/111956-solved-using-onchange-with-php/
Share on other sites

OnChange is a client side event, php runs server side. The best you can do is set the OnChange event to execute a Javascript function which in turn calls the XMLHttpRequest object which can make a request to the server and execute your php code. This methodology is the basis of Ajax, and that my good friend is where you want to start looking.

OnChange is a client side event, php runs server side. The best you can do is set the OnChange event to execute a Javascript function which in turn calls the XMLHttpRequest object which can make a request to the server and execute your php code. This methodology is the basis of Ajax, and that my good friend is where you want to start looking.

 

Definitely... or if you are not interested with Ajax as of the moment... then reload the page with a value submitted, giving the new skin to take effect.

I would avoid using AJAX in this situation because you have to reload the page once the cookie is set anyways for it to come into effect.  This is how I do it:

 

<?

if ($_POST['submit']) {

setcookie(MHQ_skin, $_POST[skin], time()+(60*60*24*365));

header('location:somepage.php');

}

?>

<form action="" method="post">

<select name="skin">

<option value='3year'>3rd Anniversary Skin</option>

<option value='orig'>Original Skin</option>

</select>

<input type="submit" value="submit" name="submit" />

</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.