Connecting Tech Pros Worldwide Forums | Help | Site Map

Passing Form Field Values to PHP Page

Newbie
 
Join Date: Oct 2007
Posts: 27
#1: Oct 1 '07
I wonder if anyone could explain how I pass the field values in my request form to the PHP processor page. My external javascript file is as follows:

Expand|Select|Wrap|Line Numbers
  1. var sections = ["rqust", "sendr", "reset", "cancel"];
  2.  
  3. for (var i=0; i < sections.length; i++) 
  4.     {
  5.     eval(sections[i] + '1 = new Image(100,22)');
  6.     eval(sections[i] + '1.src = "images/btn_' + sections[i] + '1.jpg"');
  7.      eval(sections[i] + '2 = new Image(100,22)');
  8.      eval(sections[i] + '2.src = "images/btn_' + sections[i] + '2.jpg"');
  9.      eval(sections[i] + '3 = new Image(100,22)');
  10.     eval(sections[i] + '3.src = "images/btn_' + sections[i] + '3.jpg"');
  11.     eval(sections[i] + '4 = new Image(64,22)');
  12.     eval(sections[i] + '4.src = "images/btn_' + sections[i] + '4.jpg"');
  13.     eval(sections[i] + '5 = new Image(64,22)');
  14.     eval(sections[i] + '5.src = "images/btn_' + sections[i] + '5.jpg"');
  15.     eval(sections[i] + '6 = new Image(64,22)');
  16.     eval(sections[i] + '6.src = "images/btn_' + sections[i] + '6.jpg"');
  17.     }
  18.  
  19. function switchImage(imgDocID,imgObjName) 
  20.     {
  21.     document[imgDocID].src = eval(imgObjName + ".src");
  22.     }
  23.  
  24. function doSubmit() 
  25.     {
  26.     window.location='<pathtoprocessor/page.php>'
  27.     }
  28.  
  29. function doReset() 
  30.     {
  31.     self.focus();
  32.  
  33.     document.TheForm.lastname.value = "";
  34.     document.TheForm.firstname.value = "";
  35.     document.TheForm.jobtitle.value = "";
  36.     document.TheForm.cmp_org.value = "";
  37.     document.TheForm.email.value = "";
  38.     }
When I click on the Submit button the error page is shown and seems to indicate that fields have not been filled in when, in fact, they have.

The code for the form is as follows:

[HTML] <form action="sbx_rqustproc.php" name="TheForm" method="post" class="form">

<table border="0" cellspacing=0 cellpadding=0>

<tr>
<td class="firstlabel" width="130">Last Name:</td>
<td><input type="text" class="formtext" name="thelastname" id="lastname" size="20" />&nbsp;<span class="required">*</span></td>
</tr>
<tr>
<td class="label" width="130">First Name:</td>
<td class="formfield"><input type="text" class="formtext" name="thefirstname" id="firstname" size="20" />&nbsp;<span class="required">*</span></td>
</tr>
<tr>
<td class="label" width="130">Job Title:</td>
<td class="formfield"><input type="text" class="formtext" name="thejobtitle" id="jobtitle" size="20" />&nbsp;<span class="required">*</span></td>
</tr>
<tr>
<td class="label" width="130">Company/Org.:</td>
<td class="formfield"><input type="text" class="formtext" name="thecmp_org" id="cmp_org" size="20" />&nbsp;<span class="required">*</span></td>
</tr>
<tr>
<td class="label" width="130">Email Address:</td>
<td class="formfield"><input type="text" class="formtext" name="theemail" id="email" size="30" />&nbsp;<span class="required">*</span></td>
</tr>
<tr>
<td class="label" width="130">Talk Requested:</td>
<td class="formfield"><input type="text" class="formtalk" value="<?php echo $mytalk; ?>" name="thetalk" id="talk" size="20" readonly /></td>
</tr>
</table>

<table border="0" cellspacing=0 cellpadding=0>
<tr>
<div class="formbtns" align="center">

<!-- // Standard Submit Button Code
<input type="submit" name="Submit" value="Submit">
-->

<a href="javascript:doSubmit();"
onmouseover="switchImage('sendr', 'sendr5')"
onmouseout="switchImage('sendr', 'sendr6')">

<img src="images/btn_sendr4.jpg"
width="64" height="22" name="sendr" border="0" alt="Submit Button">

</a>

<a href="javascript:doReset();"
onmouseover="switchImage('reset', 'reset5')"
onmouseout="switchImage('reset', 'reset6')">

<img src="images/btn_reset4.jpg"
width="64" height="22" name="reset" border="0" alt="Reset Button">

</a>

<a href="javascript:history.back()"
onmouseover="switchImage('cancel', 'cancel5')"
onmouseout="switchImage('cancel', 'cancel6')">

<img src="images/btn_cancel4.jpg"
width="64" height="22" name="cancel" border="0" alt="Cancel Button">

</a>

</div></td>
</tr>
</table>

</form>[/HTML]

If I use the commented out Standard Submit Button Code the form is processed as expected but the submit button does not follow the rollover and style required.

Do I need to add some code to the doSubmit() function to send the form values to the php processor page? Can anyone explain what I need to do to make this work?

Any help with this would be appreciated. Thanks in advance.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Oct 1 '07

re: Passing Form Field Values to PHP Page


Moved from Articles section.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#3: Oct 1 '07

re: Passing Form Field Values to PHP Page


In the doSubmit() function, submit the form rather than redirecting the page:
Expand|Select|Wrap|Line Numbers
  1. document.forms["TheForm"].submit();
Newbie
 
Join Date: Oct 2007
Posts: 27
#4: Oct 1 '07

re: Passing Form Field Values to PHP Page


Thanks for your reply acoder and apologies for posting the thread to the wrong place. I'll try to be more careful in future.

I have changed the external javascript file as you suugested:

Expand|Select|Wrap|Line Numbers
  1. var sections = ["rqust", "sendr", "reset", "cancel"];
  2.  
  3. for (var i=0; i < sections.length; i++) 
  4.     {
  5.     eval(sections[i] + '1 = new Image(100,22)');
  6.     eval(sections[i] + '1.src = "images/btn_' + sections[i] + '1.jpg"');
  7.      eval(sections[i] + '2 = new Image(100,22)');
  8.      eval(sections[i] + '2.src = "images/btn_' + sections[i] + '2.jpg"');
  9.      eval(sections[i] + '3 = new Image(100,22)');
  10.     eval(sections[i] + '3.src = "images/btn_' + sections[i] + '3.jpg"');
  11.     eval(sections[i] + '4 = new Image(64,22)');
  12.     eval(sections[i] + '4.src = "images/btn_' + sections[i] + '4.jpg"');
  13.     eval(sections[i] + '5 = new Image(64,22)');
  14.     eval(sections[i] + '5.src = "images/btn_' + sections[i] + '5.jpg"');
  15.     eval(sections[i] + '6 = new Image(64,22)');
  16.     eval(sections[i] + '6.src = "images/btn_' + sections[i] + '6.jpg"');
  17.     }
  18.  
  19. function switchImage(imgDocID,imgObjName) 
  20.     {
  21.     document[imgDocID].src = eval(imgObjName + ".src");
  22.     }
  23.  
  24. function doSubmit() 
  25.     {
  26.     document.forms["TheForm"].submit();
  27.     }
  28.  
  29. function doReset() 
  30.     {
  31.     self.focus();
  32.  
  33.     document.TheForm.lastname.value = "";
  34.     document.TheForm.firstname.value = "";
  35.     document.TheForm.jobtitle.value = "";
  36.     document.TheForm.cmp_org.value = "";
  37.     document.TheForm.email.value = "";
  38.     }
The rollovers are all working but the Submit button returns the error page indicating that the required fields have not been filled in as before. It appears the form values are still not being passed.

Have I implemented what you suggested correctly or am I still doing something wrong?

Thanks again for your help.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#5: Oct 1 '07

re: Passing Form Field Values to PHP Page


See how you can style the submit button here.
Newbie
 
Join Date: Oct 2007
Posts: 27
#6: Oct 1 '07

re: Passing Form Field Values to PHP Page


Thanks again for your reply, acoder, and the link you have provided to the information about styling a submit button. Armed with this I should be able to sort things out.

Very much appreciated.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#7: Oct 2 '07

re: Passing Form Field Values to PHP Page


No problem. Let us know if you manage to solve the problem.
Newbie
 
Join Date: Oct 2007
Posts: 27
#8: Oct 2 '07

re: Passing Form Field Values to PHP Page


Acoder, thanks for all your valuable help. The reference material you pointed me to made interesting reading in the early hours of this morning! I managed to fix the issue in the end having realised that some of the changes I had made to the code on the advice of others had confused the whole issue. Renaming the form field name attribute certainly hadn't helped!

What I have ended up with which appears to work without any problems is as follows:

javascript file:

Expand|Select|Wrap|Line Numbers
  1. var sections = ["rqust", "sendr", "reset", "cancel"];
  2.  
  3. for (var i=0; i < sections.length; i++) 
  4.     {
  5.     eval(sections[i] + '1 = new Image(100,22)');
  6.     eval(sections[i] + '1.src = "images/btn_' + sections[i] + '1.jpg"');
  7.      eval(sections[i] + '2 = new Image(100,22)');
  8.      eval(sections[i] + '2.src = "images/btn_' + sections[i] + '2.jpg"');
  9.      eval(sections[i] + '3 = new Image(100,22)');
  10.     eval(sections[i] + '3.src = "images/btn_' + sections[i] + '3.jpg"');
  11.     eval(sections[i] + '4 = new Image(64,22)');
  12.     eval(sections[i] + '4.src = "images/btn_' + sections[i] + '4.jpg"');
  13.     eval(sections[i] + '5 = new Image(64,22)');
  14.     eval(sections[i] + '5.src = "images/btn_' + sections[i] + '5.jpg"');
  15.     eval(sections[i] + '6 = new Image(64,22)');
  16.     eval(sections[i] + '6.src = "images/btn_' + sections[i] + '6.jpg"');
  17.     }
  18.  
  19. function switchImage(imgDocID,imgObjName) 
  20.     {
  21.     document[imgDocID].src = eval(imgObjName + ".src");
  22.     }
  23.  
  24. function doSubmit() 
  25.     {
  26.     document.forms["TheForm"].submit();
  27.     }
  28.  
  29. function doReset() 
  30.     {
  31.     self.focus();
  32.  
  33.     document.TheForm.lastname.value = "";
  34.     document.TheForm.firstname.value = "";
  35.     document.TheForm.jobtitle.value = "";
  36.     document.TheForm.cmp_org.value = "";
  37.     document.TheForm.email.value = "";
  38.     }    
  39.  
html form:

[HTML] <form action="sbx_rqustproc.php" name="TheForm" id="TheForm" method="post" class="form">

<table border="0" cellspacing=0 cellpadding=0>

<tr>
<td class="firstlabel" width="130">Last Name:</td>
<td><input type="text" class="formtext" name="lastname" id="lastname" size="20" />&nbsp;<span class="required">*</span></td>
</tr>
<tr>
<td class="label" width="130">First Name:</td>
<td class="formfield"><input type="text" class="formtext" name="firstname" id="firstname" size="20" />&nbsp;<span class="required">*</span></td>

</tr>
<tr>
<td class="label" width="130">Job Title:</td>
<td class="formfield"><input type="text" class="formtext" name="jobtitle" id="jobtitle" size="20" />&nbsp;<span class="required">*</span></td>
</tr>
<tr>
<td class="label" width="130">Company/Org.:</td>
<td class="formfield"><input type="text" class="formtext" name="cmp_org" id="cmp_org" size="20" />&nbsp;<span class="required">*</span></td>

</tr>
<tr>
<td class="label" width="130">Email Address:</td>
<td class="formfield"><input type="text" class="formtext" name="email" id="email" size="30" />&nbsp;<span class="required">*</span></td>
</tr>
<tr>
<td class="label" width="130">Talk Requested:</td>
<td class="formfield"><input type="text" class="formtalk" value="" name="talk" id="talk" size="20" readonly /></td>

</tr>
</table>

<table class="formbtns" align="center" border="0" cellspacing=0 cellpadding=0>
<tr>
<td>

<a href="javascript:doSubmit();"
onmouseover="switchImage('sendr', 'sendr5')"
onmouseout="switchImage('sendr', 'sendr6')">

<img src="images/btn_sendr4.jpg"
width="64" height="22" name="sendr" border="0" alt="Submit Button">

</a>

<a href="javascript:doReset();"
onmouseover="switchImage('reset', 'reset5')"
onmouseout="switchImage('reset', 'reset6')">

<img src="images/btn_reset4.jpg"
width="64" height="22" name="reset" border="0" alt="Reset Button">

</a>

<a href="javascript:history.back()"
onmouseover="switchImage('cancel', 'cancel5')"
onmouseout="switchImage('cancel', 'cancel6')">

<img src="images/btn_cancel4.jpg"
width="64" height="22" name="cancel" border="0" alt="Cancel Button">

</a>

</td>
</tr>
</table>


</form>
[/HTML]

The thing I like about this solution (- provided I haven't fallen foul of any other issues!) is that the there is a consistency in the way the buttons are coded and handled. I hope this makes sense.

Many thanks again for your patience and help.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#9: Oct 2 '07

re: Passing Form Field Values to PHP Page


Glad to hear that you got it working. Post again if you hit any more problems.
Reply