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.

morehawes

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

10+ Year Member



Hi and welcome to Webmaster World!

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


Be careful that the logic used to calculate your quote does not rely on on values passed through HTML forms as these are very easy to manipulate... but that might not matter in your case.

If you are looking to achieve this :

<input name="make" type="hidden" value="3 Months" /> 


You could define a function like this :


<?php
function programme_value_to_text($value) {
$text = array(
'7.00' = > '3 Months',
'11.00' = > '6 Months',
'85.00' = > '12 Months',
'900.00' = > '24 Months'
);
if(array_key_exists($value, $text)) {
return $text[$value];
} else {
return '';
}
}
?>


and then call this function to insert the correct text in your form :


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


Ideally you would want to take this further so the original dropdown menu uses the $text array to build the <option>s in teh first place but this is just an example of what you could do.

Hope that is of some help.

pmmarketing

10:29 am on Aug 11, 2010 (gmt 0)

10+ Year Member



Hi morehawes,

thank you for your response. It's sort of what I am wanting to do, but not fully?

I am trying to use the select form to select an option, go through to the next page which will show the selected option as a quote and then the selected option to be shown as hidden variable but with the original "value" for example

<option value="7.00" id="3" name="3 months">3 Months</option>


to show 7.00 in the quote and 3 months if this option is chosen in a hidden text field.

I hope this makes sense :), again thank you for a quick response :D.

morehawes

10:47 am on Aug 11, 2010 (gmt 0)

10+ Year Member



Hi pmmarketing, in that case I am not 100% sure what you are trying to achieve. The <option> HTML tag does not allow the name attribute (Spec here : [w3schools.com...] so this would be ignored when you submit the form.

However in your PHP script you can easily link up the value "7.00" to "3 Months" as I demonstrated in my code above - so i'm not sure why you need to pass the two values between scripts.

As you mentioned above storing values in the session might make passing values between scripts easier. For example you could define the variables in your session like this :


$_SESSION['foo'] = '7.00';
$_SESSION['bar'] = '3 months';


and then be able to access them from any script where you re-create the session. This means you don't have to worry about re-passing variables through a form. This link might be a good place to start if you are thinking about using sessions : [uk.php.net...] there are even some simple examples.

Good luck!

pmmarketing

12:12 pm on Aug 11, 2010 (gmt 0)

10+ Year Member



hi morehawes, thank you for a quick response.

I think everything has got confused :).

Thank you for mentioning the "names" bit, what I am trying to do is:

1) Have a user select
<option value="7.00">3 Months</option>


and then on post to a new page for this to show them the quote, which will show

7.00 on it's own.

I then want to have something saying "if this is correct, please click on submit" which will send variables to the next page.

The variable should show which option they had chosen, but show this as "3 month".

The php of the 1st page can be found here:

<?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"];


?>




<div id="twocolbLong">
<div id="tcbloLeft">
<div class="bluelongBlock">
<div class="fbTitle">Caravans -<br />
Extended Warranty</div>
<p>Hello <?php echo "$title"; echo " $sur" ?> please find your quote for your Caravan Warranty below.</p>
<div class="quoTeb"><?php echo "$quote";?></div>
<p>If you are happy to go ahead with this quote, please click on submit below.</p>

<!-- form -->
<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";?>"/>

<input type="image" src="button.png" name="image" style="padding:10px 0px 0px 35px;" />

</form>
<!-- form -->
</div>
</div>
</div>
<div id="tcbloRight">&nbsp;</div>



I hope this makes sense, thanks :).

pmmarketing

9:32 am on Aug 12, 2010 (gmt 0)

10+ Year Member



Sorry to bump this up, can anyone help as this is the only part holding me up on my site. I just can't work out how to show the part:

"6 months"

 <option value="11.00">6 Months</option>


on the page where the form is posted too.

Thanks to anyone who helps.

omoutop

10:12 am on Aug 12, 2010 (gmt 0)

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



On your first page:
<option value="7.00">3 Months</option>
The [7.00] and the [3 Months] are taken from a database or are manually typed in the page?

pmmarketing

10:17 am on Aug 12, 2010 (gmt 0)

10+ Year Member



Hi omoutop, they are manually typed.

omoutop

10:35 am on Aug 12, 2010 (gmt 0)

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



ok then
try something like this:
$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>';
}
This will create your options in the first page but with the month as value (instead for the actual price).
Copy this array to your next page (or include it in any way you like).
On your next page:
$month = $_POST['month_field']; // or what you call it
$value = $price_array[$month];
So now you have everything you need
I hope this works.. typed it on the fly

pmmarketing

12:34 pm on Aug 12, 2010 (gmt 0)

10+ Year Member



Hi omoutop, thank you. I know this is a silly question, hope can I show the final option on the 2nd page.

Would it be possible to show the price they chose and then the option they chose if this makes sense. I think this is what the code you have given me does, but I am not 100% sure.

Thanks again :).

Orangutang

12:34 pm on Aug 12, 2010 (gmt 0)

10+ Year Member



Hi pmmarketing,

I'm relatively new to php myself but I built a quotes section that uses forms, variables, php and sessions. I've looked at your site and think I understand but can I clarify your objective

1. User arrives at warranty form, fills out, submits.
2. Site displays confirmation of the form with another submit button to proceed if there happy.

Your problem is in echoing out exactly what you want from the initial submit. Eg: 7 instead of 3 months

Have I understood correctly ?

omoutop

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

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




$month = $_POST['month_field']; // or what you call it
$value = $price_array[$month];


Yes you are correct. Short example:

echo $month.' Months';

This will print something like "6 Months"

 echo $value;

This will print something like "11.00"

With these two you can do whatever you want in your second page

pmmarketing

1:48 pm on Aug 12, 2010 (gmt 0)

10+ Year Member



Hi omoutop, thanks for the response.

I have tried to script and you can find the first part here:
[gatewayleisuregroup.com ]

and the 2nd page here:
[gatewayleisuregroup.com ]

The code I have on page 2 is below, but it doesn't seem to work. What am i missing?

<?php 

$month = $_POST['month_field']; // or what you call it
$value = $price_array[$month];

echo $month.' Months';

echo $value; ?>



Thanks for all your help :).

omoutop

1:50 pm on Aug 12, 2010 (gmt 0)

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



your field is called 'programme', so in your second page:
$month = $_POST['programme'];

pmmarketing

1:51 pm on Aug 12, 2010 (gmt 0)

10+ Year Member



Hi Orangutang thanks for trying to help too :).

yes you are right.

1) they choose the programme they would like
2) it shows the cost of the programme and then in a hidden field shows the programme as for examples 3 months.
3) Once submit is clicked this will send the chosen programme to a new page.

omoutop

1:52 pm on Aug 12, 2010 (gmt 0)

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



ok then it works
glad i could help

pmmarketing

1:55 pm on Aug 12, 2010 (gmt 0)

10+ Year Member



Oh, hehe I am so dumb sometimes. Thanks omoutop.

It kind of works, the working example can be seen in the link below:
http://gatewayleisuregroup.com/new/warrantys/index.php [gatewayleisuregroup.com]


I see how it works, it is almost their but I want it to do one small thing.

i want it to show a quote for example if 3 months is chosen and the cost for this is £7.

then it would say something like:

Your quote is £7 and then in a hidden field I want it to show 3 months.

This was my original select form, if this helps :).
<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>


Thanks again :)

Orangutang

1:57 pm on Aug 12, 2010 (gmt 0)

10+ Year Member



Hi omoutop and pmmarketing,

Apologies for butting in on this one but I'm learning myself and this is helping so hopefully you don't mind.

omoutop is creating 2 variables for the data that you need. $month and $value.

$price_array[3] = '7.00';

This is creating a variable called $price_array which represents the value of [3] in this case and is equal to 7.00

foreach ($price_array as $month=>$value)

This is looping through the $price_arrays and creating a new variable $month for the value of the $price_array which = 3 and another new variable called $value which = 7.00

Therefore you now have 2 new variables to use however you want.

$month = Actual months = 3
$value = Value of actual month = 7.00

You can then echo them out as stated:

echo $month.' Months';
This will print something like "6 Months"

echo $value;
This will print something like "11.00"

As said I'm learning myself so if I have misunderstood something I'd appreciate it being pointed out.

pmmarketing

2:00 pm on Aug 12, 2010 (gmt 0)

10+ Year Member



After reading organutang's post it explains it more, I see the price is set in the index file I have, but I can't get it to show the price with on the 2nd page with the code I have.

The code for the first page is below:
<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>



(Please ignore the div tags)

Orangutang

2:11 pm on Aug 12, 2010 (gmt 0)

10+ Year Member



I'm so slow it looks as though its solved.

Your quote is £7 and then in a hidden field I want it to show 3 months.

I've looked at the page and I see what your echoing out.

As omoutop stated, once you have your variables you can do whatever you want. Below i echo both of them out in a sentence.

Your quote is £<?php echo "$value"?> for the <?php echo "$months" month period.?>

This will write - Your quote is £7 for the 3 month period.

Hopefully my syntax is ok and hopefully you see where I'm coming from.

pmmarketing

3:42 pm on Aug 12, 2010 (gmt 0)

10+ Year Member



With the code i have it still won't show the values.

These are the codes for both pages:

Page 1 :
<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>



page 2:
<?php 

$month = $_POST['programme'];
$value = $price_array[$month];

echo $month.' Months';

echo $value; ?>


What am i missing?

Orangutang

4:31 pm on Aug 12, 2010 (gmt 0)

10+ Year Member



Hiya,

I think I see whats happening. I'll try a few things and post back in a bit.

Orangutang

4:54 pm on Aug 12, 2010 (gmt 0)

10+ Year Member



Bear with me, got to go out. Have created your 2 pages and am testing.

I need to learn this as well, we'll get there :-)

Orangutang

6:50 pm on Aug 12, 2010 (gmt 0)

10+ Year Member



Hiya,

Not quite sorted but nearer. If you change page 2 to below it echos out correctly all the months with the prices.

<?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;
}

?>

But this is the problem, I want it to only echo out the choosen one.

If you delete the variable I've highlighted in bold nothing changes on the output so the problem may be to do with that because thats not right.

Hopefully its helped a bit and I'll have a look over the weekend, as said I need to learn this as well.

Have a good one :-)

omoutop

5:47 am on Aug 13, 2010 (gmt 0)

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



on the second page...
- 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)

If you do this on top of your page, you can use the $month, $value variables throughout your page inany way you like:
echo them for user to see them on his screen
echo them as hidden fields values
store them in your database/flat file

What part is confusing you?

Orangutang

9:28 am on Aug 13, 2010 (gmt 0)

10+ Year Member



Its this bit ? :-)

first declare your array ($price_array)

I'm not sure how to do this ? I understand declaring variables but haven't declared an array.

Could you let me know exactly I need to write at the top of page2. How do I declare the array to use ?

Cheers :-)

enigma1

9:43 am on Aug 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You must have somewhere a declaration statement like

$price_array = array();

you need the declaration or an assignment before start loading it.

Orangutang

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

10+ Year Member



Hi,

I see what your saying and understand thanks. You'll have to forgive me for not understanding but I still cannot get it to work ?

If I copy $price_array = array(); to the top of the page nothing changes.

So I thought the brackets are empty, ie, I'm trying to declare an empty array so I tried this $price_array = array($price_array); but all this did was start the echoed array at 0 instead of 3 ?

I've tried every combination I can think of with no success. Theres only 2 pages to the site so I'm sure I'm overlooking something very simple but I can't make it work.

Any help would be much appreciated :-)

omoutop

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

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



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

This must exist in both pages somehow.
Either type it 2 times OR load it from a txt file or include it with a php file... it doesn matter really.
You need the full array (with the values) in both pages.

pmmarketing

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

10+ Year Member



what would be the final code? I am confused now :). Thanks for all your help though as I think we are really close with the final code :).
This 39 message thread spans 2 pages: 39