Page 1 of 1

Submitting data from hidden form fields.

Posted: Tue Dec 09, 2008 11:01 am
by hairytea
Hi,

I am using javascript to give values to hidden form fields and then using php to collect that info!

my javascript code....

Code: Select all

 
<script type="text/javascript" language="javascript">
<!--
window.onload = pagesRequired;
 
//The following four variables are my hidden form fields...
var Quote = document.form1.Quote.value;
var CMS = document.form1.CMS.value;
var Flash = document.form1.Flash.value;
var PagesHolder = document.form1.Pages.value;
 
 
//Loop through the radio buttons to which one is checked and set a value to the hidden form fields!
function pagesRequired() {
    var len = document.form1.pages.length;
    for(var i = 0; i < len; i++) {
      if (document.form1.pages[i].checked == true) {
        if (i == 0) {
          PagesHolder = "1-4";
        }
        else if (i == 1) {
          PagesHolder = "5-10";
        }
        else if (i == 2) {
          PagesHolder = "11-15";
        }
        else if (i == 3) {
          PagesHolder = "16-20";
        }
      }
    }
}
 
//-->
</script>
 

And, here is the php code...

Code: Select all

 
To: root@xxxxxxxxxxx
From: root@xxxxxxxx
Subject: xxxxxxxxxxx - Online Quote
 
Name            : [r_Name]<br/>
Email address   : [re_Email_address]<br/>
Sent : [%DATE_GMT]
<br/><br/>
----------------------------------------------------------------------
<br/><br/>
<strong>Thank you for requsting an online quote.</strong>
<br/><br/>
Based upon the information you provided we have calculated a price of <strong>GBP [Quote]</strong>.
<br/><br/>
The above price is based upon the following....
<br/><br/>
Web Pages             : [Pages]<br/>
Content Management    : [CMS]<br/>
Flash Content         : [Flash]<br/>
 
 
The php generates an email and sends it on ok......the [r_Name] and [re_Email_Address] field values are sent in the email no problem (they are visible form fields)!

I know the javascript IS working because i added a function to alert to the screen the current value of the hidden form fields....i.e. if i presed the radio button 5-10, then clicked the button, it alerted the correct amount!

So i know for sure when radio buttons are pressed that the hidden field was 100% being updated!

When i submit the form it sends an email with an empty value from the hidden form field, even though it has a value that my javascript passed to it!

Any ideas guys?

Re: Submitting data from hidden form fields.

Posted: Tue Dec 09, 2008 11:08 am
by watson516
I might be missing something here but I see no php code.

Your first code snippet is javascript but the second one appears to be just a part of an html file with bad tagging.

Re: Submitting data from hidden form fields.

Posted: Tue Dec 09, 2008 11:32 am
by hairytea
it is the server side .txt file that php calls.....sorry should have explained!

it is part of a php captcha script.....the php calls this and another .txt file ... one to send an email to the user and one to send an email to myself.

fyi all html in my site validates so you won't find any code with bad tagging. :lol:

Re: Submitting data from hidden form fields.

Posted: Tue Dec 09, 2008 12:15 pm
by Reviresco
We need to see the form itself and the php code... Not enough here to work with.

Re: Submitting data from hidden form fields.

Posted: Tue Dec 09, 2008 2:24 pm
by hairytea

Code: Select all

 
<form id="form1" name="form1" method="post" action="contact_master.php">
      <input type="hidden" name="Pages" id="Pages" value="1-4" />
      <input type="hidden" name="CMS" id="CMS" value="" />
      <input type="hidden" name="Flash" id="Flash" value="" />
      <input type="hidden" name="Quote" id="Quote" value="" />
      <table width="475" border="0">
  <tr>
    <td colspan="2"><strong>How many pages do you require?</strong></td>
    </tr>
  <tr>
    <td colspan="2"><input type="radio" name="pages" tabindex="1" onclick="pagesRequired();" checked="checked" />1 - 4&nbsp;&nbsp;<input type="radio" name="pages" tabindex="2" onclick="pagesRequired();" />5 - 10&nbsp;&nbsp;<input type="radio" name="pages" tabindex="3" onclick="pagesRequired();" />11 - 15&nbsp;&nbsp;<input type="radio" name="pages" tabindex="4" onclick="pagesRequired();" />16 - 20</td>
    </tr>
  <tr>
    <td colspan="2"><strong>Content Managed Site?</strong></td>
    </tr>
  <tr>
    <td width="118"><input type="radio" name="cms" value="yes" tabindex="5" />Yes&nbsp;&nbsp;<input type="radio" name="cms" value="yes" tabindex="6" checked="checked" />
      No</td>
    <td><img src="../images/info.jpg" alt="Click here for More Information!" width="15" height="15" /></td>
    </tr>
  <tr>
    <td colspan="2"><strong>Flash Animated Content?</strong></td>
    </tr>
  <tr>
    <td><input type="radio" name="flash" value="yes" tabindex="7" />Yes&nbsp;&nbsp;<input type="radio" name="flash" value="yes" tabindex="8" checked="checked" />No</td>
    <td><img src="../images/info.jpg" alt="Click here for More Information!" width="15" height="15" /></td>
    </tr>
  <tr>
    <td colspan="2"><strong>Please provide your details:-</strong></td>
  </tr>
  <tr>
    <td colspan="2">Name * <br/><input name="r_Name" type="text" class="textfield" id="r_Name" size="30" style="background-color:#000066" /></td>
    </tr>
  <tr>
    <td colspan="2">Email * <br/><input name="re_Email_address" type="text" class="textfield" id="re_Email_address" size="30" style="background-color:#000066" /></td>
    </tr>
  <tr>
    <td colspan="2">Image code<br/><span class="captchaborder"><img src="contact_master.php?captcha" alt="Captcha Image" /></span><br/>Enter Image Code <br />
      (CAPITAL letters only)<br/><input name="r_captcha" type="text" class="textfield" onfocus="doClear(this)" style="background-color:#000066"/>
      <br />
      <input name="image222322" type="image" class="btnborder" src="images/send_btn.gif" alt="send" /></td>
    </tr>
</table>
    </form>
 

Re: Submitting data from hidden form fields.

Posted: Tue Dec 09, 2008 3:28 pm
by hairytea
does anyone know if this is possible?

i came up with idea, but it seems as though the form is posting the data set when the page first loads, and does not pass the data after javascript has changed it??

any kind of suggestion greatly appreciated :D

Re: Submitting data from hidden form fields.

Posted: Wed Dec 10, 2008 3:29 am
by hairytea
i can only assume this is not a process that will work then???

would setting cookies work?

Re: Submitting data from hidden form fields.

Posted: Fri Dec 12, 2008 8:12 am
by hairytea
It took me two day to write the script but it is now working and well worth the effort!

The javascript sets cookies based upon user input and then refreshes the page.
Once the page has been refreshed and loaded, the hidden form fields values are filled with the value of the cookies stored.
Then php picks up the values from the hidden form fields and passes them to be processed (i.e. sent in an email).

IT WORKS!!!! :lol: :D :o :wink:

those interested here is all the javascript i wrote...

Code: Select all

 
<script type="text/javascript" language="javascript">
<!--
 
window.onload = checkCookies; 
 
function checkCookies() {
  var hitCt = cookieVal("pageHit");
  document.form1.Pages.value = hitCt;
  if (hitCt == "1-4") {
    document.form1.pages[0].checked = true;
  }
  else if (hitCt == "5-10") {
    document.form1.pages[1].checked = true;
  }
  else if (hitCt == "11-15") {
    document.form1.pages[2].checked = true;
  }
  else if (hitCt == "16-20") {
    document.form1.pages[3].checked = true;
  }
  else if (hitCt == "") {
    document.form1.Pages.value = "1-4";
  }
  
  var cmsCt = cookieVal("CMS");
  document.form1.CMS.value = cmsCt;
  if (cmsCt == "Yes") {
    document.form1.cms[0].checked = true;
  }
  else if (cmsCt == "No") {
    document.form1.cms[1].checked = true;
  }
  else if (cmsCt == "") {
    document.form1.cms.value = "No";
  }
  
  var flashCt = cookieVal("Flash");
  document.form1.Flash.value = flashCt;
  if (flashCt == "Yes") {
    document.form1.flash[0].checked = true;
  }
  else if (flashCt == "No") {
    document.form1.flash[1].checked = true;
  }
  else if (flashCt == "") {
    document.form1.Flash.value = "No";
  }
}
 
function pagesRequired() {
    var pagesHolder = "1-4";
    var len = document.form1.pages.length;
    for(var i = 0; i < len; i++) {
      if (document.form1.pages[i].checked == true) {
        if (i == 0) {
          PagesHolder = "1-4";
          setCookie(PagesHolder);
        }
        else if (i == 1) {
          PagesHolder = "5-10";
          setCookie(PagesHolder);
        }
        else if (i == 2) {
          PagesHolder = "11-15";
          setCookie(PagesHolder);
        }
        else if (i == 3) {
          PagesHolder = "16-20";
          setCookie(PagesHolder);
        }
      }
    }
}
 
function setCookie(numOfPages) {
    var expireDate = new Date();
    expireDate.setMinutes(expireDate.getMinutes()+300);
 
    document.cookie = "pageHit=" + numOfPages + ";expires=" + expireDate.toGMTString();
    location.href = "index.php";
}
 
function cmsRequired() {
    var cmsHolder = "No";
    var len = document.form1.cms.length;
    for(var i = 0; i < len; i++) {
      if (document.form1.cms[i].checked == true) {
        if (i == 0) {
          cmsHolder = "Yes";
          setCookiecms(cmsHolder);
        }
        else if (i == 1) {
          cmsHolder = "No";
          setCookiecms(cmsHolder);
        }
      }
    }
}
 
function setCookiecms(cmsDecision) {
    var expireDate = new Date();
    expireDate.setMinutes(expireDate.getMinutes()+300);
 
    document.cookie = "CMS=" + cmsDecision + ";expires=" + expireDate.toGMTString();
    location.href = "index.php";
}
 
function flashRequired() {
    var flashHolder = "No";
    var len = document.form1.flash.length;
    for(var i = 0; i < len; i++) {
      if (document.form1.flash[i].checked == true) {
        if (i == 0) {
          flashHolder = "Yes";
          setCookieflash(flashHolder);
        }
        else if (i == 1) {
          flashHolder = "No";
          setCookieflash(flashHolder);
        }
      }
    }
}
 
function setCookieflash(flashDecision) {
    var expireDate = new Date();
    expireDate.setMinutes(expireDate.getMinutes()+300);
 
    document.cookie = "Flash=" + flashDecision + ";expires=" + expireDate.toGMTString();
    location.href = "index.php";
}
 
function cookieVal(cookieName) {
  var thisCookie = document.cookie.split("; ");
  
  for (var i = 0; i < thisCookie.length; i++) {
      if (thisCookie[i].split("=")[0] == cookieName) {
          return thisCookie[i].split("=")[1];
      }
  }
  return 0;
}
//-->
</script>