Jump to content

[SOLVED] Need help with Displaying Checkboxes output after chosen


Recommended Posts

Hello,

 

This is the Problem I am having. after checking the boxes I want when I click the submit button it on the output page it only displays "1" value of the checkboxes I chose ... if you dont understand what I mean just try it yourself here and you will see the obvious problem .

 

http://JneroCorp.com/find.php

 

Its supposed to display all the values the you checked on the checkboxes on page find2.php but it only displays one.

 

 

I just need a little help with the check boxes this is what I have so far:

 

This is "find.php":


<?php if(!isset($_POST['submit'])) { ?>

<div style="border: 2px #bbe2ef solid; background: #f0fbff; padding: 12px; width: 270px; margin: 25px auto 0 auto;">
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">

Choose a site to search:<br>

<input type="radio" name="site" value="rapidshare.com/users/" /> RapidShare.com
<br />
<input type="radio" name="site" value="rapidshare.de" /> Rapidshare.de
<br />
<input type="radio" name="site" value="megaupload.com" /> MegaUpload.com
<br />
<input type="radio" name="site" value="HotFile.com" /> HotFile.com
<br />
<input type="radio" name="site" value="filedropper.com" /> FileDropper.com
<br />
<input type="radio" name="site" value="fileqube.com" /> FileQube.com
<br />
<input type="radio" name="site" value="rapidshare.de" /> Rapidshare.de
<br />
<input type="radio" name="site" value="myfreefilehosting.com" /> MyFreeFileHosting.com
<br />
<input type="radio" name="site" value="fileden.com" /> FileDen.com
<br />
<input type="radio" name="site" value="easy-share.com" /> Easy-Share.com
<br />
<input type="radio" name="site" value="filefactory.com" /> FileFactory.com
<br />
<input type="radio" name="site" value="uploading.com" /> Uploading.com

<br>

<input name="submit" type="submit" value="Next Step!" />
</form>
</div>
<?php

} else {

$site = $_POST['site']

?>

<div style="border: 2px #bbe2ef solid; background: #f0fbff; padding: 12px; width: 270px; margin: 25px auto 0 auto;">
<form method="post" action="./find2.php">

Choose File Types to Search:<br>
<input name="site" type="hidden" value='<?php echo "$site"; ?>'>

<input type="checkbox" name="filetype" value=".txt+OR+"> .txt<br>
<input type="checkbox" name="filetype" value=".bmp+OR+"> .bmp<br>
<input type="checkbox" name="filetype" value=".gif+OR+"> .gif<br>
<input type="checkbox" name="filetype " value=".jpg+OR+"> .jpg<br>
<input type="checkbox" name="filetype" value=".png+OR+"> .png<br>
<input type="checkbox" name="filetype" value=".psd+OR+"> .psd<br>
<input type="checkbox" name="filetype" value=".psp+OR+"> .psp<br>
<input type="checkbox" name="filetype" value=".pdf+OR+"> .pdf<br>
<input type="checkbox" name="filetype" value=".mp3+OR+"> .mp3<br>
<input type="checkbox" name="filetype" value=".wav+OR+"> .wav<br>
<input type="checkbox" name="filetype" value=".wma+OR+"> .wma<br>
<input type="checkbox" name="filetype" value=".avi+OR+"> .avi<br>
<input type="checkbox" name="filetype" value=".flv+OR+"> .flv<br>
<input type="checkbox" name="filetype" value=".mov+OR+"> .mov<br>
<input type="checkbox" name="filetype" value=".mp4+OR+"> .mp4<br>
<input type="checkbox" name="filetype" value=".swf+OR+"> .swf<br>
<input type="checkbox" name="filetype" value=".wmv+OR+"> .wmv<br>
<input type="checkbox" name="filetype" value=".exe+OR+"> .exe<br>
<input type="checkbox" name="filetype" value=".gz+OR+"> .gz<br>
<input type="checkbox" name="filetype" value=".pkg+OR+"> .pkg<br>
<input type="checkbox" name="filetype" value=".rar+OR+"> .rar<br>
<input type="checkbox" name="filetype" value=".zip+OR+"> .zip<br>
<input type="checkbox" name="filetype" value=".iso+OR+"> .iso<br>
<input type="checkbox" name="filetype" value=".torrent+OR+"> .torrent<br>
<br>

<input name="submit2" type="submit" value="Search!" />
</form>
</div>
<?php

} 

?>

 

and this is "find2.php" (The Page where all the inputs are output):


<?php

$site = $_POST['site'];
$filetypes = $_POST['filetype'];

echo "The Site you selected is $site and the Filetypes are $filetypes";

?>

Hi,

You have the same name for all the checkboxes so it will only return the last one that is checked.

 

You need to change them into an array and then access the array values in find2.php.

<input type="checkbox" name="filetype[]" value=".txt+OR+"> .txt<br>
<input type="checkbox" name="filetype[]" value=".bmp+OR+"> .bmp<br>
<input type="checkbox" name="filetype[]" value=".gif+OR+"> .gif<br>
<input type="checkbox" name="filetype[]" value=".jpg+OR+"> .jpg<br>

 

 

Chris

once you submit the form you can see the array data for testing like this:

 

print_r($_POST["filetype"]);

This, in itself is not much use, just useful for showing the data that has been passed.

 

How to use it would depend on what you want to do with the results.

However, basically you need to loop through it to break it up into "usefull" data, something like this:

if(count($_POST["filetype"]>0)){
  foreach($_POST["filetype"] AS $key=>$val){
    echo "Filetype selected: ".$val."<br>";
  }
}

That will just give you a list of the filetypes checked, so you need to modify the line

echo "Filetype selected: ".$val."<br>";

to get the code to do what you want it to with the user submitted data.

 

Chris

ok this is very helpful so far this is almost exactly what i want it to do but

you see how each value for the checkboxes are like ".wav+OR+" and ".zip+OR+"

 

Well they need to be displayed like this

 

http://jnerocorp.com/?q=$filetypes$url    <-- That needs to display something that would look like this

 

http://JneroCorp.com/?q=.wav+OR+.zip+OR+.rar+OR+site:http://rapidshare.com/ 

 

so $filetypes will display = ".wav+OR+.zip+OR+.rar+OR+"

and

$url will display = "site:http://rapidshare.com/"

 

all together in 1 link

 

I just dont know how to display the link correctly all together

OK, so you need to create a new variable rather than echoing the $var.

Try this:

// define filetypes from array
$filetypes="";
if(count($_POST["filetype"]>0)){
foreach($_POST["filetype"] AS $key=>$val){
    $filetypes.=$val;
  }
}
// define site url
$url='site:'.$_POST["site"];

//put it together to make url
$new_url='http://JneroCorp.com/?q='.$filetypes.$url;

Personally I would consider removing the +OR+ part from the posted checkbox values and add them in the loop, but that won't actually make any difference.

 

Chris

Thanks so much chris this is absoloutly great but is it possible to make it so that the values dont have the +OR+ on each filetype and is there a way to make it so say the user check 3 boxes

 

I need it to display like this http://JneroCorp.com/?q=.zip+OR+.rar+OR+.txt+site:rapidshare.com

 

it needs to be so that there is no +OR+ after the last filetype it needs to be just +

 

So this is what it currently looks like:

http://JneroCorp.com/?q=.zip+OR+.rar+OR+.txt+OR+site:rapidshare.com

 

This is what I need it to look like:

http://JneroCorp.com/?q=.zip+OR+.rar+OR+.txt+site:rapidshare.com

 

 

 

 

 

 

$filetypes="";
if(count($_POST["filetype"]>0)){
foreach($_POST["filetype"] AS $key=>$val){
    $filetypes.=$val;
  }
$filetypes=substr($filetypes,0,-3);
}

should strip the last 3 characters off

 

Chris

Give this a go. Much more manageable than what you have now. Let me know if you have any questions.

 

<?php

$site_list = array(
    ' RapidShare.com'  => 'rapidshare.com/users/', ' Rapidshare.de'   => 'rapidshare.de',
    ' MegaUpload.com'  => 'megaupload.com', ' HotFile.com'     => 'HotFile.com',
    ' FileDropper.com' => 'filedropper.com', ' FileQube.com'    => 'fileqube.com',
    ' Rapidshare.de'   => 'rapidshare.de', ' MyFreeFileHosting.com' => 'myfreefilehosting.com',
    ' FileDen.com'     => 'fileden.com', ' Easy-Share.com'  => 'easy-share.com',
    ' FileFactory.com' => 'filefactory.com', ' Uploading.com'   => 'uploading.com',
);

$filetype_list = array (
    '.txt', '.bmp', '.gif', '.jpg', '.png', '.psd', '.psp', '.pdf', '.mp3', 
    '.wav', '.wma', '.avi', '.flv', '.mov', '.mp4', '.swf', '.wmv', '.exe', 
    '.gz', '.pkg', '.rar', '.zip', '.iso', '.torrent'
);

$form = false;
$output = '';

if(!isset($_POST['site']))
{
    $form = "Choose a site to search::<br />\n";
    //Create radio group for site selection
    foreach($site_list as $siteName => $siteURL)
    {
        $form .= "<input type=\"radio\" name=\"site\" value=\"{$siteURL}\" /> {$siteName}<br />\n";
    }
}
elseif(!isset($_POST['filetypes']))
{
    $form = "Choose File Types to Search:<br />\n";
    $form .= "<input name=\"site\" type=\"hidden\" value=\"{$_POST['site']}\">";
    //Create checkboxes for file type selection
    foreach($filetype_list as $fileType)
    {
        $form .= "<input type=\"checkbox\" name=\"filetypes[]\" value=\"{$fileType}\"> {$fileType}<br />\n";
    }
}
else
{
    $typesAry = $_POST['filetypes'];
    $link = "http://JneroCorp.com/?q=" . implode('+OR+', $typesAry) . "+OR+site:http://{$_POST['site']}/";
    $output  = "The Site you selected is: $site<br />\n";
    $output .= "The Filetypes selected are: " . implode(', ', $typesAry) . "<br /><br />\n";
    $output .= "Link: <a href=\"{$link}\">{$link}</a>\n";
}

if ($form!=false)
{
    $output = "
        <div style=\"border: 2px #bbe2ef solid; background: #f0fbff; padding: 12px; width: 270px; margin: 25px auto 0 auto;\">
        <form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">
        {$form}
        <br />
        <input name=\"submit\" type=\"submit\" value=\"Next Step!\" />
        </form>
        </div>";
}

?>
<html>
<head></head>
<body>
<?php echo $output; ?>
</body>
</html>

WOW that code is great mjdamato but I realized there was one problem on the link that is created

 

EXAMPLE:

 

http://JneroCorp.com/?q=.txt+OR+.bmp+OR+.gif+OR+.jpg+OR+.png+OR+.psd+OR+.psp+OR+site:http://rapidshare.com/users//

 

Needs to look like this

 

http://JneroCorp.com/?q=.txt+OR+.bmp+OR+.gif+OR+.jpg+OR+.png+OR+.psd+OR+.psp+site:http://rapidshare.com/users//

 

after the last filetype needs to be just a + not a +OR+

 

Thanks so much,

John

If you take a look at the code, you will see that the +OR+ has been added manually:

$link = "http://JneroCorp.com/?q=" . implode('+OR+', $typesAry) . "+OR+site:http://{$_POST['site']}/";

so just change that to this:

$link = "http://JneroCorp.com/?q=" . implode('+OR+', $typesAry) . "+site:http://{$_POST['site']}/";

 

Chris.

PS. using the implode() is clearly a better way to go, I just didn't want to get into too much detail of changing the original code ;)

Yeah, I misread your examples. It didn't make sense to me whythere should be an OR there, but I was just following what I "thought" you asked for. cbolson's fix should do the trick.

 

I also noticed another minor bug. I made a last minute change and didn't make all the needed changes for that. change this line

	$output  = "The Site you selected is: $site<br />\n";

 

To this in order for the site name to be displayed

	$output  = "The Site you selected is: {$_POST['site']}<br />\n";

 

I primarily rewrote the code to hopefully show some better techniques to use and a more fluid flow for the logic. For example, by putting the values into arrays you can easily modify the selections on the page by simply adding/editing/removing values from the arrays - the logic is totally unaffected. Likewise, you'll notice I did not intermingle the logic and the HTML. The logic does all the comparisons and prepatory work and creates variables as needed. Personally, I would probably break the code into three different files using includes() as needed. I would put the arrays into a single file (or maybe a file for each). And, I would put the html code into it's own file. You would then have three different files for data, logic, and the display.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.