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.

Need help with my contactform...

Featured Replies

Hi! I have a contactform validated php...

When you type in letters in the phone field there is an error that tells you that you are only allowed to use numbers.

 

To my problem... when this error end the other arises they are in times new roman and behind the form.. how can i get these in a pop up or something like that!?

 

Help is appreciated!

  • Author

Javascript alert should do it http://www.w3schools.com/js/js_popup.asp

 

Use something like:

 


echo "<script type=\"text/javascript\">alert('You can only use numbers');</script>";

 

 

this is my code:

<?php
if(isset($_POST['name']) && isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['message'])){
$errors = array();
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];

if(empty($name)){
$errors[] = "Ad your name!";
}
if(empty($phone)){
$errors[] = "Fill your number!";
}
if(!is_numeric($phone)){
$errors[] = " value="Only numbers please!";
}
if(empty($email)){
$errors[] = "Fill a valid Email";
}
if(empty($message)){
$errors[] = "Please type a message!";
}

if(!empty($errors)){
foreach($errors as $error){
echo $error, "<br />";
}
} else{
ini_set("SMTP", "smtp.trondan.se");
$to = "trondan@live.se";
$subject = "From trondan";
$headers = "From: ".$email;
$body = "Email From:$name  --  Number: $phone  --  Email: $email  --  Message: $message";

mail($to, $subject, $body, $headers);
echo "Message sent!";


}

}
?>
<html>
<head>
<link href="http://trondan.se/erik/form.css" media="all" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function restoreValue(obj){
if(obj.value == ""){
obj.value = obj.defaultValue;
}
}
function clearValue(obj){
if(obj.value == obj.defaultValue){
obj.value = "";
}
} </script>
</head>
<body>
<form action="" method="POST">
<font style="font-size:13px" color="#C0C0C0" face="Arial">
<input type="text" name="name" onfocus="clearValue(this);" onblur="restoreValue(this);" style="position:absolute;left:79px;top:15px;width:351px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:2" name="Name" value="Name">
<input type="email" name="email" onfocus="clearValue(this);" onblur="restoreValue(this);"style="position:absolute;left:79px;top:59px;width:351px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:4" name="email" value="Email">
<input type="text" name="phone" onfocus="clearValue(this);" onblur="restoreValue(this);" style="position:absolute;left:79px;top:103px;width:351px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:7" name="phone" value="Phone">
<textarea name="message" id="Text" onfocus="clearValue(this);" onblur="restoreValue(this);" style="position:absolute;left:80px;top:147px;width:350px;height:270px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:6" rows="13" cols="31">Message</textarea>
<input id="submit" type="submit" value="Contact me" style="position:absolute;left:310px;top:425px;width:120px;height:40px;z-index:23">
</font>
</form>
</body>
</html>

 

And you see this:

 

$errors = array();
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];

if(empty($name)){
$errors[] = "Ad your name!";
}
if(empty($phone)){
$errors[] = "Fill your number!";
}
if(!is_numeric($phone)){
$errors[] = " value="Only numbers please!";
}
if(empty($email)){
$errors[] = "Fill a valid Email";
}
if(empty($message)){
$errors[] = "Please type a message!";
}

Those alerts are the one I want to be in a pop up...

can I use that code for that?

What's this line all about?

 

if(!is_numeric($phone)){
$errors[] = " value="Only numbers please!";
}

To put your errors into a pop up you would need to use

 

if(!empty($errors)) {
// JavaScript popup
 echo "<script type=\"text/javascript\">alert(";
 foreach($errors as $error){
   echo "'".$error."' + '\\n' + ";
 }
 echo "'');</script>";

// NoScript, in case javascript is disabled
 echo "<noscript>";
 foreach($errors as $error){
   echo $error, "<br />";
 }  
 echo "</noscript>";
}

  • Author

To put your errors into a pop up you would need to use

 

if(!empty($errors)) {
// JavaScript popup
 echo "<script type=\"text/javascript\">alert(";
 foreach($errors as $error){
   echo "'".$error."' + '\\n' + ";
 }
 echo "'');</script>";

// NoScript, in case javascript is disabled
 echo "<noscript>";
 foreach($errors as $error){
   echo $error, "<br />";
 }  
 echo "</noscript>";
}

 

 

 

Where would i put that?

Pretty new with php.. :)

To put your errors into a pop up you would need to use

 

if(!empty($errors)) {
// JavaScript popup
 echo "<script type=\"text/javascript\">alert(";
 foreach($errors as $error){
   echo "'".$error."' + '\\n' + ";
 }
 echo "'');</script>";

// NoScript, in case javascript is disabled
 echo "<noscript>";
 foreach($errors as $error){
   echo $error, "<br />";
 }  
 echo "</noscript>";
}

 

Just as a side note. No offence but this is a mess, I would never dream of outputting javascript like this and I'm sure advanced developers will tell u the same thing.

  • Author

Just as a side note. No offence but this is a mess, I would never dream of outputting javascript like this and I'm sure advanced developers will tell u the same thing.

 

 

What to do then?

Looking back at the code, it does look messy and there are better ways of doing it, but they would require a lot more modification to the code, this is just a quick short term solution I made in a short amount of time.

 

 

This is how it would be implemented into your code –

 

<?php
if(isset($_POST['name']) && isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['message'])){
$errors = array();
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];

if(empty($name)){
$errors[] = "Ad your name!";
}
if(empty($phone)){
$errors[] = "Fill your number!";
}
if(!is_numeric($phone)){
$errors[] = "Only numbers please!";
}
if(empty($email)){
$errors[] = "Fill a valid Email";
}
if(empty($message)){
$errors[] = "Please type a message!";
}

if(!empty($errors)) {
// JavaScript popup
 echo "<script type=\"text/javascript\">alert('";
 foreach($errors as $error){
   echo $error."\\n";
 }
 echo "');</script>";

// NoScript
 echo "<noscript>";
 foreach($errors as $error){
   echo $error, "<br />";
 }  
 echo "</noscript>";
} 

} else{
ini_set("SMTP", "smtp.trondan.se");
$to = "trondan@live.se";
$subject = "From trondan";
$headers = "From: ".$email;
$body = "Email From:$name  --  Number: $phone  --  Email: $email  --  Message: $message";

mail($to, $subject, $body, $headers);
echo "Message sent!";


}

}
?>
<html>
<head>
<link href="http://trondan.se/erik/form.css" media="all" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function restoreValue(obj){
if(obj.value == ""){
obj.value = obj.defaultValue;
}
}
function clearValue(obj){
if(obj.value == obj.defaultValue){
obj.value = "";
}
} </script>
</head>
<body>
<form action="" method="POST">
<font style="font-size:13px" color="#C0C0C0" face="Arial">
<input type="text" name="name" onfocus="clearValue(this);" onblur="restoreValue(this);" style="position:absolute;left:79px;top:15px;width:351px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:2" name="Name" value="Name">
<input type="email" name="email" onfocus="clearValue(this);" onblur="restoreValue(this);"style="position:absolute;left:79px;top:59px;width:351px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:4" name="email" value="Email">
<input type="text" name="phone" onfocus="clearValue(this);" onblur="restoreValue(this);" style="position:absolute;left:79px;top:103px;width:351px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:7" name="phone" value="Phone">
<textarea name="message" id="Text" onfocus="clearValue(this);" onblur="restoreValue(this);" style="position:absolute;left:80px;top:147px;width:350px;height:270px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:6" rows="13" cols="31">Message</textarea>
<input id="submit" type="submit" value="Contact me" style="position:absolute;left:310px;top:425px;width:120px;height:40px;z-index:23">
</font>
</form>
</body>
</html>

  • Author

That does not seem to work..

Parse error: syntax error, unexpected T_STRING in /storage/content/trondan.se/public_html/test.php on line 16

If it's that line on your original code, it's because you are using ' " ' inside your error, when you are using the same thing to define its contents

 

Change

 

$errors[] = " value="Only numbers please!";

 

To something like

 

$errors[] = "Only numbers please!";

 

If it's not that you've probably missed out the semicolon at the end, or you're missing a '{' or '}' somewhere

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.