Solved

php check boxes and url

Posted on 2011-09-15
10
241 Views
Last Modified: 2012-05-12
This is the code for the checkbox input in the page body:

<input type="checkbox" name="custname<?php echo $row_orders['ID']; ?>" id="custname<?php echo $row_orders['ID']; ?>" value="1" <?php if ($_SESSION[base64_encode($row_orders['ID'])] == 1) { ?>checked="checked"<?php } ?>/>



This is the code for generating the url:

//generate CSV link url
$custname = "unchecked";

if (isset($_GET['custname'])) {

if ($custname == '1') {
$custname = 'checked';

}

}
$csv_url2 = "p=1&";
do {
if (!empty($_SESSION[base64_encode($row_ordersb[ID])])) {
      $csv_url2 .= "cn[$row_ordersb[CustName]]&";
      }
} while ($row_ordersb = mysql_fetch_assoc($ordersb));
$csv_url2 .= $csv_url2."p=1";

What I need is to be able to uncheck the "custname" checkbox for certain customers and this will stop that name from being included in the url. At the moment ALL names are being submitted in the url.
0
Comment
Question by:doctorbill
  • 5
  • 5
10 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 36544367
You might want to learn about HEREDOC notation in PHP.  It is much easier to use than something like this.

<input type="checkbox" name="custname<?php echo $row_orders['ID']; ?>" id="custname<?php echo $row_orders['ID']; ?>" value="1" <?php if ($_SESSION[base64_encode($row_orders['ID'])] == 1) { ?>checked="checked"<?php } ?>/>

The bold part of that input control is what you would want to change.  Not sure how you might determine who the "certain customer" would be, but I am guessing that you could use an if() statement, testing some variable.
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 36544435
See http://www.laprbass.com/RAY_temp_doctorbill.php
Outputs
<input type="checkbox" name="custname3" id="custname3" value="1" />

Here is the man page ref about HEREDOC.  It's very useful for separating code from presentation.
http://us3.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
<?php // RAY_temp_doctorbill.php
error_reporting(E_ALL ^ E_NOTICE);


// FROM EE
/* <input type="checkbox" name="custname<?php echo $row_orders['ID']; ?>" id="custname<?php echo $row_orders['ID']; ?>" value="1" <?php if ($_SESSION[base64_encode($row_orders['ID'])] == 1) { ?>checked="checked"<?php } ?>/> */


// SOME TEST DATA
$row_orders['ID'] = 3;


// SETTING LOCAL VARIABLES FOR USE IN HEREDOC BLOCK
$c = 'custname' . $row_orders['ID'];
$i = 'custname' . $row_orders['ID'];
$x = NULL;
if ($_SESSION[base64_encode($row_orders['ID'])] == 1) $x = ' checked="checked" ';

// USING HEREDOC TO MAKE IT CLEARER AND EASIER TO WORK WITH
$input = <<<ENDINPUT
<input type="checkbox" name="$c" id="$i" value="1" $x />
ENDINPUT;

echo htmlentities($input);

Open in new window

0
 

Author Comment

by:doctorbill
ID: 36544608
Sorry - this:
<input type="checkbox" name="custname<?php echo $row_orders['ID']; ?>" id="custname<?php echo $row_orders['ID']; ?>" value="1" <?php if ($_SESSION[base64_encode($row_orders['ID'])] == 1) { ?>checked="checked"<?php } ?>/>

Should be this:
<input type="checkbox" name="custname<?php echo $row_orders['ID']; ?>" id="custname<?php echo $row_orders['ID']; ?>" value="1" />
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:doctorbill
ID: 36544743
Please see my edit directly above

Also, how do I usew this to build the url I mentioned ?
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36544764
I think we need a little more explanation of the part about "build the URL"  What information do you want to put into the GET request?  Where does it come from?
0
 

Author Comment

by:doctorbill
ID: 36544871
This is the checkbox input:
<input type="checkbox" name="custname<?php echo $row_orders['ID']; ?>" id="custname<?php echo $row_orders['ID']; ?>" value="1" />

This generates the url - I use the "$csv_url2 .= $csv_url2."p=1";" which works fine. What I simply need is to be able to dictate the customer name which is used in the url by the checkbox. ie if the checkbox is checked, that customer name is used in the url. Obviously the first part of the script below is not correct. It would be helpful to me if you can adapt this script rather than using a new one

//generate CSV link url
$custname = "unchecked";

if (isset($_GET['custname'])) {

if ($custname == '1') {
$custname = 'checked';

}

}
$csv_url2 = "p=1&";
do {
if (!empty($_SESSION[base64_encode($row_ordersb[ID])])) {
      $csv_url2 .= "cn[$row_ordersb[CustName]]&";
      }
} while ($row_ordersb = mysql_fetch_assoc($ordersb));
$csv_url2 .= $csv_url2."p=1";
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36545527
There is not enough of a script there to adapt, and I would only be guessing about where the data comes from and where it goes to.  Can you please post the entire script (please use the code snippet) for both the form and the action script, thanks.
0
 

Author Comment

by:doctorbill
ID: 36545742
Can you simply give me an example of how to build a url from check box data - if the check box is checked, the data is put into the url, if it is unchecked, it is not put into the url.
The script above is working for me BUT it does not recognise whether the check box is checked or not. I just need to know how to tell the script that the checkbox is either checked or not checked
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 36546124
Sure, that's easy.  Unchecked checkboxes do not come through in the request string.  Experiment with this and watch the URL.
http://www.laprbass.com/RAY_temp_doctorbill.php
<?php // RAY_temp_doctorbill.php
error_reporting(E_ALL);

// SHOW THE REQUEST ARGUMENTS, IF ANY
var_dump($_GET);

// PRESERVE THE STATE OF THE CHECKBOX
$checked = NULL;
if (!empty($_GET["c"])) $checked = 'checked';

// USING HEREDOC
$form = <<<ENDFORM
<form>
<input type="checkbox" name="c" value="foo" $checked /> CHECK THIS BOX TO SEE "foo" OR UNCHECK IT TO HIDE "foo"
<input type="submit" />
</form>
ENDFORM;

echo $form;

Open in new window

0
 

Author Closing Comment

by:doctorbill
ID: 36917896
I didn't manage to get  a working script but did learn some new methods
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question