Solved

php check boxes and url

Posted on 2011-09-15
10
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 110

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 110

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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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 110

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 110

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 110

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Lync server 2013 or Skype for business Backup Service Error ID 4049 – After File Share Migration
The viewer will learn how to count occurrences of each item in an array.
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…

759 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