Jump to content

PHP Post and Javascript?


chris.bacon

Recommended Posts

Ok folks on my form i use javascript on a dropdown box (months of the year) to show/hide following dropdown boxes using css

 

i.e

 

box1 = 1-28 days    default hide this dropdown

box2 = 1-30 days  default hide this dropdown

box3 = 1-31 days  default show this dropdown

 

the correct dropdown box will be shown and the others hidden according to the month selected

 

this works fine.

 

JavaScript

 

<script type="text/javascript">
<!--

// var small = 1-28 days
// var medium = 1-30 days
// var large = 1-31 days

function ToggleDays(id)
{
  var small = document.getElementById('short');
  var medium = document.getElementById('medium');
  var large = document.getElementById('long');
  var value = id.options[id.selectedIndex].value;
  if(value == 2)
  {
    small.style.display = "inline";
    medium.style.display = "none";
    large.style.display = "none";
  }
  if( value==2 || value==4 || value==6 || value==9 || value==11 )
      {
          if(value == 2)
            {
                small.style.display = "inline";
                medium.style.display = "none";
                large.style.display = "none";
            }
            else
                {
                    small.style.display = "none";
                    medium.style.display = "inline";
                    large.style.display = "none";
                }

      }
  else
  {
    small.style.display = "none";
    medium.style.display = "none";
    large.style.display = "inline";
  }
}

//-->
</script>

 

HTML form (part of)

<form name="feedback" action="newformverify.php" method="POST" onsubmit='return formValidator()'>

                   <fieldset>
                   <legend>Feedback</legend>

                   <p><label>Name</label>
                   <input id="name" type="text" name="name"/></p>

                   <p><label>Centre</label>
                   <input id="centre" type="text" name="centre"/></p>

                   <p><label>Date of meeting</label>

                   <select id="month" name="month" size="1" onchange="ToggleDays(this);">

                        <option value=0><?php echo date("F")?></option>
                        <option value=1>January</option>
                        <option value=2>February</option>
                        <option value=3>March</option>
                        <option value=4>April</option>
                        <option value=5>May</option>
                        <option value=6>June</option>
                        <option value=7>July</option>
                        <option value=8>August</option>
                        <option value=9>September</option>
                        <option value=10>October</option>
                        <option value=11>November</option>
                        <option value=12>December</option>
                   </select>

 

I want to use php to gather all info from form together and send in an email using the mail function.

 

however when i post from the select it posts the option value and not the text is there any way to stop this?

 

<?php
session_start();

$month=$_POST['month'];

echo ("$month"); //this displays either a 0 or 1 etc

?>

Link to comment
Share on other sites

If you supply a value for an option then it will be posted.  If not, the text will be posted.  So, as I see it you have several "options"  ;D

 

1. Change the value to use the month text which will require changing the javascript, or

2. Change the value to id so there is no value and use the id in the javascript, or

3. Create a function that is fired onsubmit() that reassigns the text to the value:

 

month.options[month.selectedIndex].value = month.options[month.selectedIndex].text;

Link to comment
Share on other sites

Nice  ;D

Thanks that really helped!

 

just a quick one

im sure this is not possible as i have never had the need but can i call two separate functions onsubmit()?

 

You can call one that then calls the other two:

 

onSubmit="return submit_check();"

 

function submit_check() {
   return func1() && func2();
}

 

Or:

 

onSubmit="return func1() && func2();"

 

 

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.