Page 1 of 1

extracting the value of radio buttons for my page

Posted: Thu Mar 02, 2006 3:59 am
by deadcells
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


hi,

i am creating a website which has a page that will allow the viewer to customise a PC. Once the options have been selected i would like to:

a) update the total cost of the pc based on the requirements. I don't know if an update is possible on the fly, i.e. as soon as the customer clicks a radio button the total is updated... although for now i don't mind having to click an update total button and displaying the new total on the same page.

b) email the requirements to myself.

I have so far created the radio buttons... same name different values etc.. and am trying to use a switch statement to test the value of the radio group. I'm not sure where to do the test (for $_POST) and how to keep the selected radio buttons selected after submitting the form to itself... instead of resetting to default.

here's some of my code:

Code: Select all

<?php $customprice = $row_Recordset3["itemPrice"]; ?>   // just getting the basic price from MySQL db

<form id="form1" name="form1" method="POST" action="$_SERVER['PHP_SELF']">   // form header

// these are some of the radio buttons

<input name="monitor" type="radio" value="radio1" checked="checked" />
        15" CRT</label></td>
<input type="radio" name="monitor" value="radio2" />
        17"CRT</label></td>
<input type="radio" name="monitor" value="radio3" />

...// and so on..


// here i'm trying to test for the radio buttons after submitting the form to itself.

<?php
	switch($_POST['monitor']){
	  case 'radio1':
           	$customprice = $customprice - 60000;
           	break;
	  case 'radio2':
          	$customprice = $customprice - 80000;
          	break;	   
	  case 'radio3':
          	$customprice = $customprice - 70000;
          	break;
	  default:
          	$customprice = $customprice; }?>
	
// outputting the new price

<?php echo number_format($customprice, 2, ".", ","); ?>

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

sorry

Posted: Thu Mar 02, 2006 4:18 am
by deadcells
sorry about that. will keep it in mind.

Posted: Thu Mar 02, 2006 7:03 am
by shiznatix
ok ill start from the bottom because I am feeling a bit backwards today. to keep the selected radio button checked (assuming that your form is submitting to the page that it itself is on) do somthing like this

Code: Select all

<input name="monitor" type="radio" value="radio1" checked="checked"  <?= (empty($_POST['monitor'] || $_POST['monitor'] == 'radio3') ? 'checked' : '') ?>>
<input type="radio" name="monitor" value="radio2" <?= (!empty($_POST['monitor'] && $_POST['monitor'] == 'radio2') ? 'checked="checked"' : '') ?>> 
<input type="radio" name="monitor" value="radio3" <?= (!empty($_POST['monitor'] && $_POST['monitor'] == 'radio3') ? 'checked="checked"' : '') ?>>
to email the stuff to yourself use the php mail() function because its easy

it is possible to do the update on the fly. read about ajax. but if you want to do it with only PHP then just have the total saved as a hidden field in your form and just keep giving that a new value after each time the submit button is hit and act on that hidden field with the other price information that is submitted with it to come up with the new price.

Posted: Thu Mar 02, 2006 9:20 am
by deadcells
Shiznatix...

thanx for the reply. I tried your code below but it does the same thing... i.e. resets to defalt once i click on the submit button.

will try your suggestion with the hidden field for the total.

thanx again.

SOLVED :)

Posted: Fri Mar 03, 2006 3:31 am
by deadcells
I got it working. Was pretty simple actually... now i feel dumb.

Thanx to Shiznatix for the heads up. Your code did work. My problem was that i had the submit button on the wrong form... i.e... it was on a completely seperate form from the raio buttons. DUH!!!

Anyway... here's the code for anyone who has a similar issue... although from passed experience i know my coding skills leave much to be desired. So use the code below at your own risk :?

Code: Select all

// this extracts the basic price from the database. I've done so because they are a number of available starting points...

<?php $customprice = $row_Recordset3["itemPrice"]; ?>

// form and radio button creation... thanks to Shiznatix the radio buttons maintain their selections

<form action= "<?php echo $PHP_SELF;?>" method="post"> 

<input name="monitor" type="radio" value="radio1" checked="checked"  <?= (empty($_POST['monitor']) || ($_POST['monitor'] == 'radio3') ? 'checked' : '') ?>>

<input type="radio" name="monitor" value="radio2" <?= (!empty($_POST['monitor']) && ($_POST['monitor'] == 'radio2') ? 'checked="checked"' : '') ?>> 

<input type="radio" name="monitor" value="radio3" <?= (!empty($_POST['monitor']) && ($_POST['monitor'] == 'radio3') ? 'checked="checked"' : '') ?>> 

<?php
	if (isset($_POST['monitor'])) {
		switch($_POST ['monitor']){
			case 'radio1':
           		$customprice = $customprice - 600000;
           		break;
			case 'radio2':
           		$customprice = $customprice - 800000;
           		break;	   
			case 'radio3':
           		$customprice = $customprice - 70000;
           		break;
			default:
           		$customprice = $customprice;}
				} else {;}	
		?>
</form>
Thanx again.