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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox checked 1

Status
Not open for further replies.

wysiwygGER01

Programmer
Feb 18, 2009
188
AU
Hi,

I've got 2 forms and would like to have the same checkboxes checked on both forms.

My first form is this:
Code:
<body>
<form method="post" action="form.php" name="brochures">
						
	<input name="to_download[]" value="sbpdff1" type="checkbox">&nbsp;Brochure 1<br>
	<input name="to_download[]" value="sbpdff2" type="checkbox">&nbsp;Brochure 2<br>

    <input type="submit" value="Ok">
</form>
</body>

After hitting submit on the first form, I go to a 2nd form below. I can't think of a way to check the same checkboxes as on the first form? I was thinking of going through the first array and create a variable with the value "checked" for each hit - but I don't think that will work.

Any suggestions are much appreciated.

The reason I have 2 forms is because on the 2nd form I'll later at contact details which need to be filled out before submitting the form.

Code:
<?php

$box[]=($_REQUEST['to_download']);

$key = array_search('pbpdff1', $box);
If ($key=true)
{
 	$test = "CHECKED";
}
else{
	$test = "";
}
    
?>
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<body>
<h1>Information Request</h1>
<form action="form.php" method="post" name="information_request">

<strong>General Brochure</strong><br>
Brochure 1 <input type="checkbox" name="to_download[]" <?php echo $test; ?>  value="pbpdff1"><br>
Brochure 2 <input type="checkbox" name="to_download[]"  value="pbpdf2">

			<input type="submit" name="send_request" value="Send request">
</form>
</body>
 
Hi
[ul]
[li]The relational operator for equality test is [tt]==[/tt]. ( [tt]=[/tt] is the assignment operator. )[/li]
[li]The [tt]array_search()[/tt] function returns the needle parameter on success, not [tt]true[/tt].[/li]
[li]The [tt][]=[/tt] assignment appends a new element to the array, in your case another array. So you will have a 2 dimensional $box array.[/li]
[li]If none of the checkboxes is checked, [tt]array_search()[/tt] will throw a warning.[/li]
[/ul]
So change the PHP part to this ( the HTML part remains unchanged ) :
PHP:
[navy]$test[/navy][teal]=[/teal][b]isset[/b][teal]([/teal][navy]$_REQUEST[/navy][teal][[/teal][green][i]'to_download'[/i][/green][teal]])[/teal] [teal]&&[/teal] [COLOR=darkgoldenrod]array_search[/color][teal]([/teal][green][i]'pbpdff1'[/i][/green][teal],[/teal][navy]$_REQUEST[/navy][teal][[/teal][green][i]'to_download'[/i][/green][teal]])!==[/teal]false[teal]?[/teal][green][i]'checked="checked"'[/i][/green][teal]:[/teal][green][i]''[/i][/green][teal];[/teal]
But personally I would use associative array and would change the HTML part like this ( the PHP part from the page start not needed anymore ) :
HTML:
Brochure [purple]1[/purple] [teal]<[/teal]input type[teal]=[/teal][green][i]"checkbox"[/i][/green] name[teal]=[/teal][green][i]"to_download[pbpdff1]"[/i][/green] [teal]<?php[/teal] [b]echo[/b] [navy]$_REQUEST[/navy][teal][[/teal][green][i]'to_download'[/i][/green][teal]][[/teal][green][i]'pbpdff1'[/i][/green][teal]]?[/teal][green][i]'checked="checked"'[/i][/green][teal]:[/teal][green][i]''[/i][/green][teal];[/teal] [teal]?>[/teal] value[teal]=[/teal][green][i]"pbpdff1"[/i][/green][teal]><[/teal]br[teal]>[/teal]             
Brochure [purple]2[/purple] [teal]<[/teal]input type[teal]=[/teal][green][i]"checkbox"[/i][/green] name[teal]=[/teal][green][i]"to_download[pbpdf2]"[/i][/green] [teal]<?php[/teal] [b]echo[/b] [navy]$_REQUEST[/navy][teal][[/teal][green][i]'to_download'[/i][/green][teal]][[/teal][green][i]'pbpdf2'[/i][/green][teal]]?[/teal][green][i]'checked="checked"'[/i][/green][teal]:[/teal][green][i]''[/i][/green][teal];[/teal] [teal]?>[/teal] value[teal]=[/teal][green][i]"pbpdf2"[/i][/green][teal]>[/teal]
Of course, the first page has to be changed accordingly. ( Why two pages with the same checkboxes, would be a question for another time. )

Feherke.
 
That worked! Thanks so much!

My second file is called form.php.
I'll add a contact form later on so visitors can leave their details.
After hitting submit on the 2nd file I'll send out an email with the entered details.
I'd like to show a confirmation page without redirecting to another file.

What I've done below shows me "Thanks! Email has been sent." at the top of my form.
But I want my form to disappear and just show the confirmation after pressing submit.
So last question: How can this be done on the same page?
Tried $PHP_SELF but still wouldn't work.

Code:
<?php
if(isset($_POST['send_request']))
    {
        echo("Thanks! Email has been sent.");
    }

$path="..\download\pdf";

$test=isset($_REQUEST['to_download']) && array_search('sbpdff1',$_REQUEST['to_download'])!==false?'checked="checked"':'';
$test2=isset($_REQUEST['to_download']) && array_search('sbpdff2',$_REQUEST['to_download'])!==false?'checked="checked"':'';

IF ($test != "")
	{
		echo $path."test.pdf";
	}
?>
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<body>
<h1>Information Request</h1>
<form action="form.php" method="post" name="information_request">
	<strong>General Brochure</strong><br>
	Brochure 1 	<input type="checkbox" name="to_download[]" <?php echo $test; ?>  value="pbpdff1"><br>
	Brochure 2 	<input type="checkbox" name="to_download[]" <?php echo $test2; ?> value="pbpdf2">
				<input type="submit" name="send_request" value="Send request">
</form>

</body>
</html>
 
Hi

To answer your question, just end it :
Code:
[b]if[/b][teal]([/teal][b]isset[/b][teal]([/teal][navy]$_POST[/navy][teal][[/teal][green][i]'send_request'[/i][/green][teal]]))[/teal]
    [teal]{[/teal]
        [b]echo[/b][teal]([/teal][green][i]"Thanks! Email has been sent."[/i][/green][teal]);[/teal]
        [highlight][b]return[/b][teal];[/teal][/highlight]
    [teal]}[/teal]
Note that you may need [tt]exit()[/tt] instead of [tt]return[/tt] in some circumstances.

Also note that outputing the content there ( with or without ending the execution after it ) will generate invalid HTML.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top