Solved

php check boxes and url

Posted on 2011-09-15
10
232 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 108

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 108

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
 

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 108

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now