Page 1 of 1
Passing a class from form to server side PHP script
Posted: Mon Aug 31, 2009 7:34 pm
by zunebuggy
In my form, I have an onclick event that lets the user choose from different styles that are set from css classes. Each class has its own unique name. This part works great. After submit, I want to send that class name to my php script.
I have tried (in my php script)
Code: Select all
$mystyle=$_POST[style_area.className];
Code: Select all
$mystyle=$_POST["style_area.className"];
Code: Select all
$mystyle=$_POST[document.getElementById('style_area').className];
Code: Select all
$mystyle=$_POST["document.getElementById('style_area').className"];
style_area is actually the name and id of a styled textarea.
None of this is working.
Thank you.
Re: Passing a class from form to server side PHP script
Posted: Mon Aug 31, 2009 8:42 pm
by paqman
$_POST only contains the values of all inputs within the form which was submitted. What is the user clicking to make the selection? You can have that update a hidden field, and read that value. If there are a few buttons with the options, make the all submits with different values (hence passing on the value which was clicked)
Re: Passing a class from form to server side PHP script
Posted: Mon Aug 31, 2009 9:05 pm
by zunebuggy
The user clicks a thumbnail image. This updates the css style of the textarea:
The onclick is:
Code: Select all
<input type=image src="mypic.png" width="32" height="32" value='Change_1' onClick="document.getElementById('style_area').className ='style1';return false" name="S1">
and the textarea is:
Code: Select all
<textarea name="style_area" rows="3" id="style_area" class="style3">
Re: Passing a class from form to server side PHP script
Posted: Mon Aug 31, 2009 9:06 pm
by zunebuggy
I can get the text in the textarea but I need to know what style they chose. I rewrite another page based on that style.
Re: Passing a class from form to server side PHP script
Posted: Mon Aug 31, 2009 9:55 pm
by zunebuggy
OK, I tried the hidden input type like this:
Code: Select all
<form onSubmit="" name="main_form" action="process.php" method="POST">
<input type="hidden" name="style" id="style" value="style3">
Code: Select all
<input type=image src="mypic.png" width="32" height="32" value='Change_1' onClick="document.getElementById('style_area').className ='style1' document.getElementById('style').value = 'style1';return false" name="S1">
I have 12 images each with an onclick event like above. Now as soon as I click on a thumbnail image the form submits. I have a submit button. I want the user to click the image which changes the style of the textarea. When they select the style they like and enter text in the textarea.. The text AND the style they selected has to be captured. So far it is only capturing the text.
The default style is style3. I thought this onclick event would change it to style1 but it doesn't. Not sure what to do.
Re: Passing a class from form to server side PHP script
Posted: Mon Aug 31, 2009 11:30 pm
by paqman
You're almost there. The thing to keep in mind is that the only things in the $_POST array will be the values of each input, nothing else. So maybe something like:
Code: Select all
<form id="theForm">
<a href="javascript:document.getElementById('theStyle').value='style1'; document.getElementById('theForm').submit();"><img src="mypic.png" width="32" height="32" name="S1"></a>
<input type="hidden" name="theStyle" value="">
</form>
I'm going with that and not the image as a submit button as IE7 took out the value assigned to image submits in the $_POST array... Seems stupid to me, but I'm sure they have their reasons. If you did want to use an image submit, rather than javascript to change the hidden field, you'll need to check the coordinates of where the user clicked the image - thats all that is now passed to $_POST with IE. I'm sure you can find that on Google quite easily