Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Process a form with javascript and php?

Featured Replies

Hey ppl.

 

I am pretty much there with my php email form but found somewhere that javascript is quicker than php for validation.

 

Is it fairly easy to use these two together to get the job done?

 

Anyone got a good resource to recommend for this please?

 

Thanks :)

 

Edit: I have put together a simple form, and the javascript (basic atm) is all working but can't get the php to work.

 

The php is here:

<?php 

$to = 'to'; //value of select list of email addresses
$subject = "Contact Us"; 
$email = $_REQUEST['email'] ; //sender's email add
$message = $_REQUEST['msg'] ; //typed message
$headers = "From: $email"; 
$sent = mail($to, $subject, $msg, $headers) ; 
if($sent) 
{print "Your mail was sent successfully"; }
else 
{print "We encountered an error sending your mail"; }
?>

 

and then I just added

 method="post" action="contact.php"

in the opening form tag after the javascript onsubmit bit.

 

It isn't working :(

Form Validation

 

If you go to that topic it has some explanations and examples of form validation,

 

Hope that helps, I'm in same shoes as you :)

  • Author

Sorry must have edited as you were typing..... thanks for posting irn3rd :)

 

Validation is sorted, not sure if my problem is the php code or the combination of the two.....

 

Any ideas anyone?

 

Thanks

There was a great youtube i learned how to do the email send to the user,

Youtube form Creation PHP and Validation i think

 

I think that is the video i used to make my first contact form with a php email sender, and dreamweaver can do form validation as well.

 

 

Hope this helps

 

Sorry if it aint related its just what i learned, and that's the way i see to do it at the moment for beginners like me :p

  • Author

Thanks Irn3rd i'll have a look. You and me in the same boat.....don't have dreamweaver tho.

 

Sorry mate, not what I'm looking for.

 

Please help me someone....I'm so close I can smell it lolol! :)

Thanks Irn3rd i'll have a look. You and me in the same boat.....don't have dreamweaver tho.

 

Sorry mate, not what I'm looking for.

 

Please help me someone....I'm so close I can smell it lolol! :)

 

Not sure if this is what you are after.

In your form action add this

onsubmit="MM_validateForm('password','','R');return document.MM_returnValue">

 

Change password to the field you want validated.

 

Then add this script to your page.

 

  <script type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
 d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
 if (val) { nm=val.name; if ((val=val.value)!="") {
   if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
	 if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
   } else if (test!='R') { num = parseFloat(val);
	 if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
	 if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
	   min=test.substring(8,p); max=test.substring(p+1);
	   if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
 } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
//-->
 </script>

 

This will check if the field is filled in before it posts to the php file.

 

This is the code dreamweaver generates so it probably wont validate.

 

HTH

 

Pat

  • Author

Thanks Pat. I'm sorry I think maybe i didn't explain properly.

 

I have sorted out the validation using javascript (put it into the form.php in body then called it (or whatever the techie speak is) inside the opening form tag. I then inserted the

method="post" action="contact.php"

and then the contact.php file code you see in post 1#.

 

But it's not sending.......

 

SOB!

Thanks Pat. I'm sorry I think maybe i didn't explain properly.

 

I have sorted out the validation using javascript (put it into the form.php in body then called it (or whatever the techie speak is) inside the opening form tag. I then inserted the

method="post" action="contact.php"

and then the contact.php file code you see in post 1#.

 

But it's not sending.......

 

SOB!

 

Can I see your form.php please

There you are Pat...thanks :)

 

Can you try this in your form.php

 

<form onsubmit="return formValidator()" method="post" action="contact.php" >
To: <select id="to" name="to" value="to"/>

<option>DaveF@fishercarpentry.co.uk</option>
<option>JohnB@fishercarpentry.co.uk</option>
<option>Steve@fishercarpentry.co.uk</option>
<option>Matt@fishercarpentry.co.uk</option>
<option>DaveP@fishercarpentry.co.uk</option>
<option>Pauline@fishercarpentry.co.uk</option>
<option>Enquiries@fishercarpentry.co.uk</option>
<option>WebMaster@fishercarpentry.co.uk</option>
</select>
<br />

Your Email Address: <input type='text' id="email" name="email" value="email" /><br />

Your Message (Between 20 and 750 characters): <textarea id='msg' value="msg" name="msg" cols="50" rows="15" ></textarea><br />
<input type="submit" name="submit" id="submit" class="btn" />



</form>

Also change your contact.php to

 

<?php

$to = $_POST['to']; //value of select list of email addresses
$subject = "Contact Us";
$email = $_POST['email']; //sender's email add
$message = $_POST['msg']; //typed message
$headers = "From: $email";
$sent = mail($to, $subject, $msg, $headers);
if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>

 

Pat

  • Author

Pat it says it's sent but I haven't recieved it....

Pat it says it's sent but I haven't recieved it....

 

Did you change tyhe contact.php

 

Pat

Have now yes.

 

Who is your host.

 

you may have to add this to your contact.php form

 

ini_set('sendmail_from', $email);

 

add this after the opening <?php tag

 

Pat

  • Author

Blimey Pat I bet you wish you'd never bothered lol......

with that last bit in it says; Notice: Undefined variable: email in /Applications/MAMP/htdocs/contact.php on line 2

 

which is where I inserted that line.

 

Is it likely to do with mamp?

 

Oh Pat yayyyyyyyyyyyyyyyyy!

 

Took that line back out and tried it again and it works.

 

MUAH MUAH MUAH!

 

So basically I needed to call them names rather than values?

Blimey Pat I bet you wish you'd never bothered lol......

with that last bit in it says; Notice: Undefined variable: email in /Applications/MAMP/htdocs/contact.php on line 2

 

which is where I inserted that line.

 

Is it likely to do with mamp?

 

Are you testing this locally, if so its unlikely mammp is set up to send e-mails, remove that last line of code I posted and try it online

 

Pat

  • Author

He he yes Pat it's done. Thank you so much!

 

Can ask one more thing? If instead of eching a msg, how do I redirect to a page..I did do it once but then the rest of the form didn't work so i deleted it DOH!

 

THANKS :wub: :wub:

Are you testing this locally, if so its unlikely mammp is set up to send e-mails, remove that last line of code I posted and try it online

 

Pat

 

Yes 'names' and also $_POST in your php

 

Glad its working :D

 

Pat

Something kike this should do it

 

<?php

$to = $_POST['to']; //value of select list of email addresses
$subject = "Contact Us";
$email = $_POST['email']; //sender's email add
$message = $_POST['msg']; //typed message
$headers = "From: $email";
$sent = mail($to, $subject, $msg, $headers);
if ($sent)
{
header( "Location: http://www.yourpage/thanks.html" );
} else {
echo 'There has been an error sending your e-mail. Please try again.';
}
?>

 

Just change the url to what you want

 

Pat

  • Author

Oh Pat I am forever in your debt.

 

Thankyou xxx

Oh Pat I am forever in your debt.

 

Thankyou xxx

 

No problem :D

 

Pat

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.