Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php check boxes and url

Posted on 2011-09-15
10
Medium Priority
?
273 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 2000 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

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 111

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 111

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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 …

721 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