Forum Moderators: coopster
Im not very good at php i can just get my head around it. Can someone point me in the right direction or help me with my form im creating.Its a basic ordering system. Its for a band and they want to sell albums online.
Basicly a person comes online, fills in their details, name, address, email that sorta thing.
Then choose their items which have radio buttons next to them.
Once that is done and it passes through my basic validation system a email is sent to the buyer which is actually an invoice, which contains the combined price of all the albums at the bottom and payment details.
Now i have gone and created the form and then it dawned on me i need to add the prices up some how so when the invoice is sent it has a total at the bottom of how much to pay.
Below is the code of how i have the form set up atm, i figured hidden fields may play a part in it, but i got no idea how to go about writting the php end of it that says ok this radio button is pressed add this and this together.
I hope i made some sense and someone can help me with this dilema
<fieldset>
<legend>Items<br>
</legend>
<p>
<label for="breathe" class="content">Breathe EP :</label>
<img src="images/breath_ep.jpg" width="150" height="163" border="1" align="middle">
<input name="radiobutton" type="radio" value="radiobutton">
$9.00 + Postage
<input name="album" type="hidden" id="album" value="$9.00" />
<br>
</p>
<p>
<label for="strange" class="content">Strange EP :</label>
<img src="images/strange_ep.jpg" width="150" height="163" border="1" align="middle">
<input name="radiobutton" type="radio" value="radiobutton">
$9.00 + Postage
<input name="album" type="hidden" id="album" value="$9.00" /></p>
<p/>
<p>
<label for="both" class="content">Both EP's :</label>
<input name="radiobutton" type="radio" value="radiobutton">
$15.00 + Postage <input name="both" type="hidden" id="both" value="$15.00" />
</p>
<p>
<label for="postage" class="content">Postage:</label>
<input name="postage" type="radio" value="radiobutton">
$3.00 Australian delivery <input name="postage1" type="hidden" id="postage1" value="$3.00" />
</p>
<p>
<label for="postage" class="content">Postage:</label>
<input name="postage" type="radio" value="radiobutton">
$10.00 International delivery <input name="postage2" type="hidden" id="postage2" value="$10.00" />
<br>
</p>
</fieldset>
You need to store the values obtained on your form in variables and then you can do whatever you want with them.
Here is a very simple exmaple from which you can elaborate:
<form action="process.php" method="post">
<p>Album1 $50 <input name="album" type="radio" value="50" /></p>
<p>Album2 $40<input name="album2" type="radio" value="40" /></p>
<input type="submit" value="submit" />
</form>
then in process.php:
<?php
$total = ($_POST['album2'] + $_POST['album']);
//then you can echo, mail or whatever you want with $total
?>
if(isset($_POST['album2'])) {
// do something
}else{
//do soemthing else
}
In this case,the isset function will check if the variable 'album2' actually contains some value. Then with the conditional if statment you can modify the behaviour of the script to perform some action if users select this option or some other action if they did not.
Here is the set up i have for the validation system im using
// Create an empty array to hold the error messages.
$arrErrors = array();
//Only validate if the Submit button was clicked.
if (!empty($_POST['Submit'])) {
// Each time there's an error, add an error message to the error array
// using the field name as the key.
if ($_POST['name']=='')
$arrErrors['name'] = 'Please provide your name.';
if ($_POST['phone']=='')
$arrErrors['phone'] = 'Please provide your phone number.';
if (!preg_match("/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/", $_POST['email']) ) {
$arrErrors['email'] = 'Please provide a valid email address.';}
if ($_POST['street']=='')
$arrErrors['street'] = 'Please type you street address.';
if ($_POST['subrub']=='')
$arrErrors['suburb'] = 'Please type you suburb.';
if ($_POST['postcode']=='')
$arrErrors['postcode'] = 'Please type you post code.';
if (count($arrErrors) == 0) {
// If the error array is empty, there were no errors.
// Insert form processing here. This just keeps getting worse... now i have to try and figure out how to display what album the user has selected with the radio button as well as add the prices together
Hope someone can help me with my stupid problems
Since both albums cost $9.00, you cannot identify wich one was selected just by looking at the value of $_POST['radiobuttonname'].
One possible solution is to give each radio button a different value:
<p>Album1<input name="album" type="radio" value="album" /></p>
<p>Album2<input name="album2" type="radio" value="album2" /></p>
Then in the page where you process the variables, you can assign the price and the name of the album:
$album_info = array (9 , 'name of album');
$album2_info = array (9, 'name of album2');
//you could also use associative arrays or even a multidimensional array but since you have only 2 albums and 2 values for each album, using 2 arrays like these might be easier.
//then you check if the values are set:
if (!isset($_POST['album'])){
$arrErrors['album'] = 'You didn\'t click my radio button.';
} else {
$album_price = $album_info[0];
$album_name = $album_info[1];
}
if (!isset($_POST['album2'])){
$arrErrors['album2'] = 'You didn\'t click my radio button.';
} else {
$album2_price = $album2_info[0];
$album2_name = $album2_info[1];
}
//now you have prices and names available
$total = ($album_price + $album2_price);
echo "You chose ", $album_name ," ", $album2_name, " album(s)<br>";
echo "Total price is $total";
Again, this is just the basic idea. If you have more album and more variables for each albums, you might need to use functions and more complex arrays.
i can make it so the validation checks to see if a radio button out of the 3 has been selected, but im back at the problem with getting the names and prices, i figure it has somthing to do with an array, but i got no idea how to set it up.
Can anyone shed some light on this situation?
What i think i have written is a script that will check to see if 1 out of the 3 checkboxes has been selected. If not put and error into an array as the rest of my validation does.
If one is selected take that one and then get the values and then move on to use them later.
Here is the code i wrote. I know it doesnt work it doesnt process right.
$album_info = array (9 , 'test');
$album2_info = array (9, 'name of album2');
if (!isset($_POST['album']))
$arrErrors['album'] = 'You didn\'t click my radio button.';
elseif (!isset($_POST['album2']))
$arrErrors['album'] = 'You didn\'t click my radio button.';
elseif (!isset($_POST['both']))
$arrErrors['album'] = 'You didn\'t click my radio button.';
else
$album2_price = $album_info2[0];
$album2_name = $album_info2[1];
$album_price = $album_info[0];
$album_name = $album_info[1]; When i go and submit to test if the validation works without any check boxes ticked it outputs as if ($album_info) one has been selected, when it hasnt at all. Then when i test it by selecting the other check box it just outputs the same thing again.
I think im on the right track but being a newbie to php i cant work it out.
Anyone got some ideas to my problem?
first you need to use some braces for your else, otherwise only the first line will be executed as part of the else. You have no braces for your other portions, this is only correct when there is a single line, as I said. You could write it with all braces as well
if (!isset($_POST['album'])) {
$arrErrors['album'] = 'You didn\'t click my radio button.';
} elseif (!isset($_POST['album2'])) {
$arrErrors['album'] = 'You didn\'t click my radio button.';
} elseif (!isset($_POST['both'])) {
$arrErrors['album'] = 'You didn\'t click my radio button.';
} else {
$album2_price = $album_info2[0];
$album2_name = $album_info2[1];
$album_price = $album_info[0];
$album_name = $album_info[1];
}
you can try a couple things, you could add a second test to each conditional statement as the name may be there but it could be empty
if (!isset($_POST['album']) ¦¦ empty($_POST['album'])) {
another you could use to look at what is actually getting posted to your script is this
echo '<pre>';
print_r($_POST);
echo '</pre>';
this will echo the whole POST array to the screen and may help you understand what it is you are trying to test.
NOTE: any ¦ characters must be replaced with real pipe characters as WebmasterWorld software breaks pipes
$album_info = array (9 , 'test');
$album2_info = array (9, 'name of album2');
if (!isset($_POST['album']) &&!isset($_POST['album2'])) {
$arrErrors['album'] = 'You didn\'t click my radio button.';
} else {
$album_price = $album_info[0];
$album_name = $album_info[1];
} only problem is i dont know how to set it up so when the 2nd check box is ticked, $album2_info is displayed. It just keeps showing $album_info
You could just add this after your else statement:
$album2_price = $album2_info[0];
$album2_name = $album2_info[1];
which is, the way I see it, back to where we started.
Perhaps you could save yourself a lot of trouble by having a look at your form.
Should you use radio buttons or checkboxes?
Should you send the values in an array (name=data[])?
$album2_price = $album2_info[0];
$album2_name = $album2_info[1];
it just outputs both albums and both prices when only 1 is selected.
Basicly want to test to see if at least 1 of a certain number (its 2 atm) of checkboxes are selected, then get the specific album name and price from the selected.
But if multiple checkboxes selected it gets the specific info for all of them etc.
You get what im trying to acheive.
This way isnt working that way. Is there another way to
This is the only thing stopping me from finishing with this and continuing with the rest of the website im working on.
if (!isset($_POST['album']) &&!isset($_POST['album2'])) {
I think you almost had it before..or I am completely missing the point. Try this:
<?php
$album_info = array (9 , 'name1');
$album2_info = array (9, 'name2');
if (!isset($_POST['album']) &&!isset($_POST['album2'])) {
echo "You have not selected any albums<br>";
exit;
}
if (isset($_POST['album'])) {
$album_price = $album_info[0];
$album_name = $album_info[1];
}
if (isset($_POST['album2'])) {
$album2_price = $album2_info[0];
$album2_name = $album2_info[1];
}
$total = ($album_price + $album2_price);
echo "You chose ", $album_name ," ", $album2_name, " album(s)<br>";
echo "Total price is $total";
?>
and your form:
<form action="process.php" method="post">
<p>Album1 $9 U2<input name="album" type="checkbox" value="album" /></p>
<p>Album2 $9 The cure <input name="album2" type="checkbox" value="album2" /></p>
<input type="submit" value="submit" />
</form>
Hope this helps.
This was the code
if (!isset($_POST['album']) &&!isset($_POST['album2'])) {
$arrErrors['album'] = 'Please provide your name.';
exit;}
but i changed it to end instead of exit and that has it working alright i think!
This is great!
Thanks for all your help. I hope i dont have any problems
[edited by: slimey at 12:26 am (utc) on May 17, 2006]