Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox variable

Status
Not open for further replies.

andyfresh

Technical User
Oct 4, 2005
33
GB
Hi all,

Im sure this is an easy one just need some fresh eyes on the script. This script is a simplified one that im working on but the theory is the same. When the check box is clicked I would like the javascript to run so that the screen redirects to website.php along with the value from the check box. However when i run this simplified script I cant seem to get the value to appear in the address bar.

Any help would be much appreciated.

Andy

Code:
<?
echo"<script language=\"JavaScript\" type=\"text/javascript\">";
echo"function rss(){;";
echo"window.location = 'website.php?C1=\'$C1\''";
echo"}";
echo"</script>";


echo'<form method="POST" action="[URL unfurl="true"]http://localhost/test/website.php?C1=\"$C1\"">';[/URL] 
echo'<input type="checkbox" name="C1" value="4" onclick="rss()">';
echo'</form>';
?>
 
Try this instead:
Code:
<?php
echo '<script language="JavaScript" type="text/javascript">';
echo 'function rss(){';
echo "document.MyForm.submit();";
echo '}';
echo '</script>';


echo '<form name="MyForm" method="GET" action="[URL unfurl="true"]http://localhost/test/website.php">';[/URL]
echo '<input type="checkbox" name="C1" value="4" onclick="rss()">';
echo '</form>';
?>
Changing the form submission method to GET means that the values of any items of the form will be sent via the url. I also changed the javascript to simply submit the form.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Maybe, but I don't think $CI was meant for the checkbox, but just a variable of the same name. In which case POST is fine, but the JS is better off with submit().
Code:
<?php
echo '<script language="JavaScript" type="text/javascript">';
echo 'function rss(){';
echo "document.MyForm.submit();";
echo '}';
echo '</script>';


echo '<form name="MyForm" method="POST" action="[URL unfurl="true"]http://localhost/test/website.php?C1=$C1">';[/URL]
echo '<input type="checkbox" name="C1" value="4" onclick="rss()">';
echo '</form>';
?>
 
another go...

i'm assuming the checkboxes are built from a script so that their iterative value is $cbox;

some notes:
* you do not need the form tags but it's better to have them. likewise, you do not need the div either but for xhtml compliance some form of block element is required.
* when echoing out javascript and predominantly html either break out of php entirely (close the tags) or use the heredoc syntax. the snip below uses heredoc. with heredoc you don't need to worry about quotation marks etc. and to include complex variables (eg arrays) include them in curly braces

Code:
echo <<<JS

<script language="JavaScript" type="text/javascript">
function rss(val){
 window.location = "website.php?C1="+val;
}
</script>
<form><div><input type="checkbox" name="C1" value="$cbox" onclick="rss(this.value)"></div></form>

JS;
 
jpadie said:
* you do not need the form tags but it's better to have them. likewise, you do not need the div either but for xhtml compliance some form of block element is required.
This is really a bad advice. Form elements like inputs are illegal anywhere but inside a form and the user agent may (although most that are in use today won't) ignore the input box altogether.
 
@Vragabond

This is really a bad advice. Form elements like inputs are illegal anywhere but inside a form

i hate to disagree with you but i think I do in this case.

Before first posting above I looked briefly through the HTML specs that i had quick access to and found nothing that prevented a control element sitting out side a form element. As I note above, for strict validation a form element needs to be within a block element (div, fieldset etc). I took the view that the form tag was not needed as the user was never going to address or interact with the form. he was just using the control as a trigger for a js event.

I also took the code below and (with appropriate DTDs in each case) validated it against the w3.org validator ( the validator approved the markup as valid xhtml in strict and transitional mode.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<div>
<input type="checkbox" value="on" name="cbox"  /> <span>Check Me</span>
</div>
</body>
</html>

since your post i was concerned that i might have misled readers so i also tried validating as html4.01 strict and transitional and xhtml 1.1. the code was reported as valid by w3.org in each case.

lastly (and just as an observation), i have never come across a browser that refuses to render a control outside of a form. it would be useful knowledge indeed if there were browsers out there that acted as you suggested they might: could you post a list of those that you know of?

likewise if the validators are wrong and the html spec does prohibit a control outside of a form element i'd really appreciate a pointer to the spec.

thanks
Justin
 
Huh, I stand corrected. I was quite certain that specs do not allow input element outside form since all specs describe these elements as form elements. But they do.

I was at least right about browsers not supporting that. Albeit it is not a browser you'll see a whole lot today (or in the future for that matter), Netscape Navigator 4 did not render form elements outside the form. Here's info on that:
 
Thanks for the link. luckily i don't worry too much about nn 4 any more... but the sooner we can get away from ie 6 the better!
 
Andy, have any of our answers solved your problem?

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Hi all,

I would like to thank you for all your help. There are a lot of good ways around this. I can see I still have a lot to learn. I have also looked into the rules around forms and such like which has been a great help.

Thank you again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top