Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php check boxes and url

Posted on 2011-09-15
10
Medium Priority
?
276 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …
Suggested Courses

886 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