Jump to content

[SOLVED] forms


wrathican

Recommended Posts

from the title this may sound simple.

 

but i have my form and everything works perfectly.

i want to add a new feature in.

 

my form is a booking form where a user can book an activity.

i have it linked to a mysql database so that there is a combo box with the titles of the activites, also in the table is the price of the activity.

what i want to happen is when the user selects one of the activities i want the price for that particular activity to be shown in a non-editable input box so that it can be passed onto the email send script. any ideas on how i can do this?

 

also i want something for a date selector.

since there are not the same amount of days in each month i want my form to be able to determine what month the user selected and then the right amount of days show in the day combo box.

for example if the user selects february in the month box, the day list should only go up to 28. then maybe when the user selects 2008 (the next year to be a leap year) the days in feb goes up to 29. suggestions would be awesome!

 

thanks for reading!

Link to comment
Share on other sites

what i want to happen is when the user selects one of the activities i want the price for that particular activity to be shown in a non-editable input box so that it can be passed onto the email send script. any ideas on how i can do this?

 

Do you want it to automatically be shown in the box as soon as they choose a new option? That would require JavaScript if you didn't want the page to have to be reloaded to show the new price in the box.

If you are just talking about wanting to store that price in a variable, then just have the option value hold the unique ID of the activity, then when they submit the form you can easily query the database to get exactly what you want.

 

also i want something for a date selector.

since there are not the same amount of days in each month i want my form to be able to determine what month the user selected and then the right amount of days show in the day combo box.

for example if the user selects february in the month box, the day list should only go up to 28. then maybe when the user selects 2008 (the next year to be a leap year) the days in feb goes up to 29.

 

Again, if you are wanting this to happen automatically without a page reload, your going to have to look into JavaScript.

Link to comment
Share on other sites

hmm ive never used any javascript before.

would it be a)heavy coding or b) a managable portion?

 

if a) im not too baothered about it then, a page reload wouldn't hurt anybody, as long as it doesnt delete the rest of the info the user has just input.

 

if b) then do you know where i could find such tutorials or help?

 

 

Link to comment
Share on other sites

You could do that with onchange option in the select box in PHP

OnChange is a JavaScript call, not PHP.

 

wrathican - If you posted what you wanted in the JavaScript forum, I am sure they could be much more help. It shouldn't take a huge amount of code to do, but it will definitely take a good understanding of JavaScript, so I would go post over there.

Link to comment
Share on other sites

There is definately at least a javascript component. You can either load up all the data from the start and just use javascript or you could accomplish this by the use of Ajax. But it's sort of a PITA.

Link to comment
Share on other sites

once the box is changed the form will refreach and $_POST['sample'] with be set

echo "<form name='myform' method='post'>";
echo "<select name='sample' onchange=form1.submit()>";
echo "<option value=''> </options>";
echo "<option value='test'>Testing</option>";
echo "</form>";

Link to comment
Share on other sites

here is the code for my 'booking form', it is included into my parent page

you will see i have used a while statement for the days output, i guess i could just use an if statement to determine what the maximum value of $day can be.

im sorry but i dont understand exactly what is going to happen here

would you be able to show me using an example from here how to write the code

and provide a more detailed explanation, or link me to somewhere i can read about it please.

is the onchange a javascript function?

 

<h1>Book a day out</h1></TD>
	</tr>
	  <tr>
	    <TD height="19" colspan="2" valign="top"><p><table width="440" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr>
    <td width="440" height="221" align="left" valign="top"><form id="form1" name="form1" method="post" action="sendmail.php?action=booking"><p>Name:<br />
        <input name="name" type="text" id="name" />
          <br />
        E-Mail:<br />
        <input name="email" type="text" id="email" />
        <br />
        House no/name:<br /><input name="house" type="text" id="house" />
        <br />
        Street:<br />
        <input name="street" type="text" id="street" />
        <br />
        Town/City:<br />
        <input name="town" type="text" id="town" />
        <br />
        Postcode:<br /><input name="postcode" type="text" id="postcode" maxlength="8" />
        <br />
        Home Telephone no:<br /><input name="home" type="text" id="home" maxlength="11" />
        <br />
        Mobile:<br /><input name="mobile" type="text" id="mobile" maxlength="11" />
        <br />
        Course/Tour Attending:<br />
	<select name="tour" id="tour">';?>
	<?php 
	$query = "SELECT * FROM cy_list";
	$result = mysql_query($query);
	while($row = mysql_fetch_array($result,MYSQL_NUM))
	{
	$name = $row[2];
        echo '<option value="' . $name . '">' . $name . '</option>';
	}
	?>
	</select><br />
        Date required:<br /><select name="day" id="day">
	<?php 
	$day = 1;
	while($day<=31)
	{
	echo "<option>" . $day . "</option>";
	$day++;
	}
?>
        </select>
	<select name="month" id="day">
          <option>Jan</option>
	  <option>Feb</option>
	  <option>Mar</option>
	  <option>Apr</option>
	  <option>May</option>
	  <option>Jun</option>
	  <option>Jul</option>
	  <option>Aug</option>
	  <option>Sep</option>
	  <option>Oct</option>
	  <option>Nov</option>
	  <option>Dec</option>
	</select>
	<select name="year" id="day">
          <option>2007</option>
	  <option>2008</option>
	</select>
        <br />
        Previous MTB experience:<br />
        <textarea name="experience" cols="30" rows="5" id="experience"></textarea>
        <br />
        General health and fitness: <br />
        <textarea name="health" cols="30" rows="5" id="health"></textarea>
        <br />
    </p>
      
        <input type="submit" name="Submit" value="Submit" /></p>
      </form></td>
  </tr>
</table></p></TD>
	</tr>

Link to comment
Share on other sites

Simple Example of onchange function below

 

<?
echo "<form name='form1' method='post'>";
echo "<select name='sample' onchange=form1.submit()>";
echo "<option value=''> </options>";
echo "<option value='1'>Check</option>";
echo "<option value='2'>Verify</option>";
echo "<option value='3'>Delete</option>";
echo "</select>";
$samplechk = $_POST['sample'];
if ($samplechk == "1") {Echo "Checking...........<br>";}
if ($samplechk == "2") {Echo "Verify.............<br>";}
if ($samplechk == "3") {Echo "Deleted............<br>";}
echo "</form>";
?>

Link to comment
Share on other sites

You're stuck in a very common problem, which is to generate a multi-page form.  The form has to be created by the server but you can't create the entire form without some initial input from the user.  Each time a form is submitted you can access the user's responses in PHP via $_POST.  All HTML form elements have a way of specifying a default value.  So this should really be a no-brainer.

 

1)  You display a form to the user.

2)  The user fills out some info and submits

3)  You re-display the form to the user, adding additional fields, and set the default values of the previously entered fields using what was in $_POST from the previous submition

4)  You continue in this fashion until all the information is supplied

 

<?php
  // Display a form in three steps

  // Check if form is submitted and we have everything
  if(isset($_POST["field1"]) &&
     isset($_POST["field2"]) &&
     isset($_POST["field2"]){
    // All three exist, so we process the form and then exit
    ProcessForm();
    exit();
  }
  // Get defaults
  $field1 = isset($_POST["field1"]) ? $_POST["field1"] : null;
  $field2 = isset($_POST["field2"]) ? $_POST["field2"] : null;
  $field3 = isset($_POST["field3"]) ? $_POST["field3"] : null;
?>
<form method="post" action="">
  <input type="text" name="field1" value="<?=$field1?>" />
  <?php if($field1 !== null){ ?>
    <input type="text" name="field2" value="<?=$field2?>" />
    <?php if($field2 !== null){ ?>
      <input type="text" name="field3" value="<?=$field3?>" />
    <?php } ?>
  <?php } ?>
  <input type="submit" name="submit" value="Submit" />
</form>

Link to comment
Share on other sites

Hey guys. Thanks for the replies.

I have been messing around with what you have suggested and i have come up with the following:

<HTML>
<BODY>
<form name="form2" method="post">
<select name="month" onchange=form2.submit()>
<?php
$month = $_POST['month'];
if($month == "Jan")
{
echo "<option selected>Jan</option>";
}else{
echo "<option>Jan</option>";
}
if($month == "Feb")
{
echo "<option selected>Feb</option>";
}else{
echo "<option>Feb</option>";
}
if($month == "Mar")
{
echo "<option selected>Mar</option>";
}else{
echo "<option>Mar</option>";
}
if($month == "Apr")
{
echo "<option selected>Apr</option>";
}else{
echo "<option>Apr</option>";
}
if($month == "May")
{
echo "<option selected>May</option>";
}else{
echo "<option>May</option>";
}
if($month == "Jun")
{
echo "<option selected>Jun</option>";
}else{
echo "<option>Jun</option>";
}
if($month == "Jul")
{
echo "<option selected>Jul</option>";
}else{
echo "<option>Jul</option>";
}
if($month == "Aug")
{
echo "<option selected>Aug</option>";
}else{
echo "<option>Aug</option>";
}
if($month == "Sep")
{
echo "<option selected>Sep</option>";
}else{
echo "<option>Sep</option>";
}
if($month == "Oct")
{
echo "<option selected>Oct/option>";
}else{
echo "<option>Oct</option>";
}
if($month == "Nov")
{
echo "<option selected>Nov</option>";
}else{
echo "<option>Nov</option>";
}
if($month == "Dec")
{
echo "<option selected>Dec</option>";
}else{
echo "<option>Dec</option>";
}
?>
</select>
<?php
if ($month == "Jan") {$maxday = 31;}
if ($month == "Feb") {$maxday = 28;}
if ($month == "Mar") {$maxday = 31;}
if ($month == "Apr") {$maxday = 30;}
if ($month == "May") {$maxday = 31;}
if ($month == "Jun") {$maxday = 30;}
if ($month == "Jul") {$maxday = 31;}
if ($month == "Aug") {$maxday = 31;}
if ($month == "Sep") {$maxday = 30;}
if ($month == "Oct") {$maxday = 31;}
if ($month == "Nov") {$maxday = 30;}
if ($month == "Dec") {$maxday = 31;}
?>
<select name="day">
<?php
$day = 1;
while($day<=$maxday)
{
echo "<option>" . $day . "</option>";
$day++;
}
?>
</select>
</BODY>
</HTML>

 

not sure that is the easiest way to make the month the user selected stay the same or not but hey, it my test :P

ive tested this (just this little piece of script)

and it seems to work fine.

one question i have though is,

if the form has an action, say finalbooking.php

will the form be submitted to that file when the users changes a value?

my guess is that it would be yes. correct me if i am wrong and please suggest how to come around this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.