Forum Moderators: coopster

Message Too Old, No Replies

Using forms to pass variables

forms, variables, php, sessions

         

pmmarketing

3:06 pm on Aug 10, 2010 (gmt 0)

10+ Year Member



Hello,

I am trying to use forms to give a quote and then send the quote number onto another script.

For example using this:

http://www.gatewayleisuregroup.com/new/warrantys/caravans_warranty.php [gatewayleisuregroup.com]

I am concentrating on this part of code:

<select name="programme">
<option value="" selected="">Choose Programme:</option>
<option value="7.00">3 Months</option>
<option value="11.00">6 Months</option>
<option value="85.00">12 Months</option>
<option value="900.00">24 Months</option>
</select>


This goes to a new page using the post method which is:

http://www.gatewayleisuregroup.com/new/warrantys/caraquote.php [gatewayleisuregroup.com]
I want to show the quote and then have a hidden value where it sends the chosen option to a new area. But I want it to say for example "3 months" if they have chosen "3 Months".

Can I do this using an id/name for example on:

<select name="programme">
<option value="" selected="">Choose Programme:</option>
<option value="7.00" id="3" name="3 months">3 Months</option>

</select>


If so, how?

The code I have used for posting the data in "caraquote.php" is below:

<?php 

//collecting variables from previous form

$quote = $_POST["programme"];

$make = $_POST["make"];$reg = $_POST["reg"];$serial = $_POST["serial"];

$title = $_POST["title"];$in = $_POST["initial"];$sur = $_POST["surname"];

$add1 = $_POST["address1"];$add2 = $_POST["address2"];$add3 = $_POST["address3"];$code = $_POST["postcode"];
$tel = $_POST["tel"];$email = $_POST["email"];


?>


<form method="post" action="testa.php">

<input name="programme" type="hidden" value="<?php echo "$quote";?>" />

<input name="make" type="hidden" value="<?php echo "$make";?>" />
<input name="reg" type="hidden" value="<?php echo "$reg";?>" />
<input name="serial" type="hidden" value="<?php echo "$serial";?>" />
<input name="title" type="hidden" value="<?php echo "$title";?>" />
<input name="initial" type="hidden" value="<?php echo "$in";?>" />
<input name="surname" type="hidden" value="<?php echo "$sur";?>" />
<input name="address1" type="hidden" value="<?php echo "$add1";?>" />
<input name="address2" type="hidden" value="<?php echo "$add2";?>" />
<input name="address3" type="hidden" value="<?php echo "$add3";?>" />
<input name="postcode" type="hidden" value="<?php echo "$code";?>" >
<input name="tel" type="hidden" value="<?php echo "$tel";?>"/>
<input name="email" type="hidden" value="<?php echo "$email";?>"/>



</form>


I hope someone can help me, thanks :). I asked someone for help before, they said to use php sessions, but I don't understand how this would work.

Orangutang

10:39 am on Aug 13, 2010 (gmt 0)

10+ Year Member



Thanks for all the help but I'm still struggling to make it work. It must be something that I'm just not understanding unyet all the help makes sense ?

Please find both pages below, these pages list all the info correctly but I only want it to echo out the choosen one.

index.php

<form method="post" action="page2.php">
<div class="formHoldc">
<div class="fhLeftc">Programme:</div>
<div class="fhRightc"><select name="programme">
<option value="" selected="">Choose Programme:</option>

<?php
$price_array[3] = '7.00';
$price_array[6] = '11.00';
$price_array[12] = '85.00';
$price_array[24] = '900.00';

foreach ($price_array as $month=>$value)
{
echo '<option value="'.$month.'">'.$month.' Months</option>';
}
?>

</select>
<br /><br />
<input type="image" src="http://www.gatewayleisuregroup.com/new/images/buttons/submitblue.png" name="image" style="padding:10px 0px 0px 35px;" />
</div>
</form>

page2.php

<?php
$month = $_POST['programme'];
$value = $price_array[$month];

$price_array[3] = '7.00';
$price_array[6] = '11.00';
$price_array[12] = '85.00';
$price_array[24] = '900.00';

foreach ($price_array as $month=>$value)
{
echo '<option value="'.$month.'">'.$month.' Months</option>';
echo $value;
}

?>

Echos out

3 Months7.006 Months11.0012 Months85.0024 Months900.00

Its great that its echoing out all the data but I only want it to echo out the selected. If I select 3 months from the form I'm trying to get it to echo out

3 Months7.00

Thanks for your patience and help so far but if you can help me overcome this last issue I'd be a happier man :-)

Is what I'm trying to do incorrect ?

pmmarketing

10:41 am on Aug 13, 2010 (gmt 0)

10+ Year Member



I am not sure if this is possible to post, I asked on another message board before this for help and I didn't get an answer till now.

This is another option, although I can't explain much about it (sorry).

A simple array database below:

For the main form:


<?php
function getDB()
{
return array(
1 => array('cost' => 7, 'months' =>3),
2 => array('cost' => 11, 'months' =>6),
3 => array('cost' => 85, 'months' =>12),
4 => array('cost' => 900, 'months' =>24)
);
}

function makeSelect($arr)
{
$str='';
foreach($arr AS $key=>$row)
{$str.='<option value="'.$key.'">'.$row['months'].' Months</option>/n';}
return $str;
}

$db=getDB();


if($_POST){echo('You selected: '.$db[$_POST['programme']]['months'].' Months');}
else
{
echo('
<form method="post" action="submit.php">

<input type="submit" />


<select name="programme">
<option value="0" selected="">Choose Programme:</option>
'.makeSelect($db).'
</select>
</form>');
}
?>



submit.php


<?php
function getDB()
{
return array(
1 => array('cost' => 7, 'months' =>3),
2 => array('cost' => 11, 'months' =>6),
3 => array('cost' => 85, 'months' =>12),
4 => array('cost' => 900, 'months' =>24)
);
}

$db=getDB();

if($_POST){echo('You selected: '.$db[$_POST['programme']]['months'].' Months');}
?>



This seems to work too :), but part of it can be included I think

omoutop

11:00 am on Aug 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




page2.php

<?php
$month = $_POST['programme'];
$value = $price_array[$month];

$price_array[3] = '7.00';
$price_array[6] = '11.00';
$price_array[12] = '85.00';
$price_array[24] = '900.00';

foreach ($price_array as $month=>$value)
{
echo '<option value="'.$month.'">'.$month.' Months</option>';
echo $value;
}

?>


Almost good but you are wrong.. you didnt read what i said


- first declare your array ($price_array)
- then get the $month value from the post data ($month = $_POST['programme']
- then you can have $value = $price_array[$month]; (7.00 for example)


So, based on this you should have in page 2:

$price_array[3] = '7.00';
$price_array[6] = '11.00';
$price_array[12] = '85.00';
$price_array[24] = '900.00';

$month = $_POST['programme']; // this is submitted from page 1
$value = $price_array[$month];

You see, if you dont declare your array values first, the $price_array[$month] will give you a blank/zero/null value


Also, in page 2, the following is of no use:
foreach ($price_array as $month=>$value)
{
echo '<option value="'.$month.'">'.$month.' Months</option>';
echo $value;
}

Delete it... whats the point of looping in your array and echoing all data? You only need the $month and $value - nothing else.

[edited by: omoutop at 11:02 am (utc) on Aug 13, 2010]

pmmarketing

11:01 am on Aug 13, 2010 (gmt 0)

10+ Year Member



Hi Orangutang,

I worked it out :D, I took away the option part for a test but if you view:

http://gatewayleisuregroup.com/new/warrantys/ [gatewayleisuregroup.com]

and

http://gatewayleisuregroup.com/new/warrantys/page2.php [gatewayleisuregroup.com]

You can see the working example.

the code for page 2 is below as I changed nothing on the first:

<?php 
$month = $_POST['programme'];
$value = $price_array[$month];

$price_array[3] = '7.00';
$price_array[6] = '11.00';
$price_array[12] = '85.00';
$price_array[24] = '900.00';

foreach ($price_array as $month=>$value)
{
echo 'You chose '.$month.' months which costs £'.$value.'<br><br>';
}

?>


it's a case of using each variable?

Thanks to everyone for all the help :) and for putting up with how much of a php noob I am :)

Orangutang

11:42 am on Aug 13, 2010 (gmt 0)

10+ Year Member



Hello,

Brilliant, thank you for your help and patience omoutop. It all works as expected and I understand why its happening. Thank you :-)

pmmarketing, I've had a look and brills. And thanks for your original post, another one off the list. Glad it works for both of us. :-)

And I'll echo out pmmarketings bottom sentence sentiments. At least I don't have to use php to echo that :-)

Thanks again.

pmmarketing

12:37 pm on Aug 13, 2010 (gmt 0)

10+ Year Member



No problem Orangutang :), glad we finally worked it out in the end.

and thanks again omoutop.

The final code I have for page2.php:


<?php

$price_array[3] = '7.00';
$price_array[6] = '11.00';
$price_array[12] = '85.00';
$price_array[24] = '900.00';

$month = $_POST['programme']; // this is submitted from page 1
$value = $price_array[$month];

echo 'Month: '.$month.' <br><br> Cost: '.$value.' ';

?>



A quick question though, I had no idea how I would explain it:

For example with the following:

<option value="3">3 Months</option>

What would the part which is highlighted be called, as I didn't know how to explain it. For example do you call it the "selection" or "option"?

Cheers

Keith :)

Orangutang

1:30 pm on Aug 13, 2010 (gmt 0)

10+ Year Member



Hi Keith,

I've got exactly the same as you for page2 but without the <br >'s :-)

<option value="3">3 Months</option>

With reference to this I would use the words selection for 3 Months and value for 3. I'm not 100% because I've also found explaining things tricky but looking at what you've got those are the words I'd use.

Best of luck with it and maybe talk again in another post.
Cheers :-)

Matthew1980

1:43 pm on Aug 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi guys,

Sorry I couldn't help myself:[w3schools.com ]

Cheers,
MRb

Orangutang

2:24 pm on Aug 13, 2010 (gmt 0)

10+ Year Member



Hi Matthew,

Always welcome, hope alls good for you and as ever thanks for the link. :-)
This 39 message thread spans 2 pages: 39