Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-26-2012, 12:02 PM   PM User | #1
countrydj
Regular Coder

 
Join Date: Nov 2011
Location: Preston, UK
Posts: 130
Thanks: 36
Thanked 0 Times in 0 Posts
countrydj is an unknown quantity at this point
How can I pick up two variables at the same time.

Hi Guys...
I have almost finidhed my cart, but I have a problem that I don'yt know how to code.
From the form:
PHP Code:
$output[] = '<tr>';  
$output[] = '<td align="center" colspan="2"><br><br><font color="#ff0000">Please select from the following</font></td>';   if ($row['accapproved'] == '1')  {
$output[] = '</tr>'
$output[] = '<tr height="30">'

$output[] = '<td align="center" colspan="2">On account: <input type="radio" name="delivery" value="account" />&nbsp;&nbsp;';
$output[] = '</td></tr>';
    }
$output[] = '<tr height="30">'
$output[] = '<td align="center" colspan="2">Cash on Delivery: <input type="radio" name="delivery" value="cash" />&nbsp;&nbsp;';
$output[] = 'Collect and Pay: <input type="radio" name="delivery" value="collect" /></td>';  
$output[] = '</tr>'
From the reeceiving page:
PHP Code:
$account="";  
if(isset(
$_POST['account'])){  
$account=$_POST['account'];
$account="Yes";
}  
$collect="";  
if(isset(
$_POST['collect'])){  
$collect=$_POST['collect'];
$collect="Yes";  
}  
$cash="";  
if(isset(
$_POST['cash'])){  
$cash=$_POST['cash']; 
$cash="Yes"

I know that I need to change the if(isset($_POST['cash'])) to if(isset($_POST['delivery']))
But the problem is that ALL the options are selected as YES.
I need to somehow select:
if(isset($_POST['cash'])) AND value = YES, in order to just select one.

I know that one solution would be to change from radio button to select box, but some idiot would select ALL options.

Can anybody advice me how to solve my problem ???

Thanks,
__________________
The MAN, The MYTH, The LEGEND:
John C
________________________________
Support your local Country Music Club
countrydj is offline   Reply With Quote
Old 11-26-2012, 12:24 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
With radio buttons they cannot select ALL of them if the "name" is the same.
They can do that with checkboxes, but not radio buttons.

See this test script.
You have already given each radio button a value in your form.
Now you just need to see which one they picked ...

PHP Code:

<?php

$option
="none";
if(
$_POST['delivery'])){
$option=$_POST['delivery'];
)

echo 
"The radio button they selected was: ".$option;

?>

EDIT: fixed typo



.

Last edited by mlseim; 11-26-2012 at 05:04 PM..
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
countrydj (11-26-2012)
Old 11-26-2012, 01:20 PM   PM User | #3
countrydj
Regular Coder

 
Join Date: Nov 2011
Location: Preston, UK
Posts: 130
Thanks: 36
Thanked 0 Times in 0 Posts
countrydj is an unknown quantity at this point
Hi mlseim..
Very many thanks for your help - ONCE AGAIN.
You actually made a slight mistake in your code. The lst bracket was ) not }
I soon managed to sort this out and then recode the rest of the script to work as I need it to.

Don't know whether you remember, but back around June this year you helped me with this project.
You have no idea how much I appreciated your help then, as well as now.
I will NEVER FORGET !!!

Thank you...
__________________
The MAN, The MYTH, The LEGEND:
John C
________________________________
Support your local Country Music Club
countrydj is offline   Reply With Quote
Old 11-26-2012, 01:26 PM   PM User | #4
countrydj
Regular Coder

 
Join Date: Nov 2011
Location: Preston, UK
Posts: 130
Thanks: 36
Thanked 0 Times in 0 Posts
countrydj is an unknown quantity at this point
Hi
I wonder if I can ask your advice.
What is the difference between:
PHP Code:
$delivery="none";
if(
$_POST['delivery']){
$delivery=$_POST['delivery'];

and
PHP Code:
$delivery="none";
if(isset(
$_POST['delivery'])){
$delivery=$_POST['delivery'];

Thanks...
__________________
The MAN, The MYTH, The LEGEND:
John C
________________________________
Support your local Country Music Club
countrydj is offline   Reply With Quote
Old 11-26-2012, 03:00 PM   PM User | #5
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
The best way to explain it ...

Upload this script, call it "test.php" and try it.
PHP Code:
<?php

// only test the variable if the form has been submitted.
if(isset($_POST['submit'])){

// try seeing the variable without using ISSET
if($_POST['x']){
echo 
"I detected ".$_POST['x']." without using ISSET <br />";
}

// try seeing the variable using ISSET
if(isset($_POST['x'])){
echo 
"I detected ".$_POST['x']." using ISSET <br />";
}
}
?>
<br /><br />
<form method="post">
1) submit with an empty box.<br />
2) submit with a zero (0) in the box.<br />
3) submit with any other number or text in the box.<br />
<input type="text" name="x">
<input type="submit" name="submit" value="submit">
</form>
It shows what happens if someone submits with a blank box, or a box with a zero (0) integer.
Important if you expect the integer zero (0) to be entered. PHP will see this as a 'false' in the IF statement.
You can combine them to see if they entered nothing. If ISSET and then if it's not empty. If nothing is
entered, you may want to do something about it ... or maybe you don't care. Depends on your specifications.

There is also the EMPTY command. I know ... it is sort of confusing.

Thinking about it now ... I probably mis-use it many times. Test your forms for all possible user entries.

Fou Lu probaby can explain it better than anyone here.

.

Last edited by mlseim; 11-26-2012 at 03:09 PM..
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
countrydj (11-26-2012)
Old 11-26-2012, 05:24 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yep, isset is simply to check the existence of a variable, and that it is not null. It otherwise does not care about the value of the variable. Input from forms cannot be null, only empty strings.
PHP Code:
if ($_POST['field']) 
If you have not posted your form, or you have provided the form with false equivalent data (array(), "", "0", false, although from a form you will never get array()), that will not pass the check and it will trigger an error notice if not posted.
PHP Code:
if (isset($_POST['field'])) 
Simply checks if we have been provided with a field called 'field'. It only cares if its not null, but as mentioned you cannot receive null from a form, only an empty string. I'd need to test to be 100% sure, but I believe if you managed to pump a null through it, Apache or IIS would convert it during the successful fields checks. Isset does not throw an error if the variable doesn't exist (as is its purpose).

With the example you have here, you can use a ternary operation for it: $delivery = isset($_POST['delivery']) ? $_POST['delivery'] : 'none';. Just like any if, you can group as many conditions as you want into it.

Empty checks that a variable exists and is an empty value (false equivalent). Although faith in my memory is kinda diminishing a bit, I'm quite sure that empty() used to trigger notice when the variable does not exist. Currently it does not. Empty can be used in place of isset if you demand that a provided value cannot be a false equivalent. This is unlike isset as isset considers "" or 0 to be valid. Empty does not. Personally, I use empty during validation phases, but not during initial verification. Don't use empty on something like a quantity, otherwise it will never pass the 0 check (for which you likely need to remove it from a cart for example instead of ignoring the input completely).

So to sum it up (which mlseim did):
PHP Code:
if ($variable// condition of the VALUE of the variable
if (isset($variable)) // $variable exists and is not null
if (empty($variable)) // $variable exists and is false equivalent 
Isset also has one advantage on empty; you can use many and all have to pass. Works well with forms:
PHP Code:
if (isset($_POST['username'], $_POST['password'])) 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
countrydj (11-26-2012)
Old 11-26-2012, 05:55 PM   PM User | #7
countrydj
Regular Coder

 
Join Date: Nov 2011
Location: Preston, UK
Posts: 130
Thanks: 36
Thanked 0 Times in 0 Posts
countrydj is an unknown quantity at this point
Very many thanks to both of you.
__________________
The MAN, The MYTH, The LEGEND:
John C
________________________________
Support your local Country Music Club
countrydj is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:58 PM.


Advertisement
Log in to turn off these ads.