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.

php issue

Featured Replies

I have a php script that is supposed to send an email, and then redirects back to the original site and displays a thankyou message... but it just reloads the page and not submitting, so there must be an issue in my script...

 

<?php 

$your_email ='email address here';

$advice = $_POST['advice'];
$name = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$country = $_POST['country'];

if(!isset($_POST['submit'])) {
?>

< html code here >

<?php
} else {
 // send email

 $to = $your_email;
	$subject="Website Submission";
	$from = $visitor_email;
	$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

	$body =
	"Name: $name\n".
	"Email: $email \n".
	"Phone #: $phone \n".
	"Country of residence: $country \n";
	"Interested in: $advice \n".

	$headers = "From: $from \r\n";
	$headers .= "Reply-To: $visitor_email \r\n";

   mail($to, $subject, $body, $headers); 
}   
?>

< html >

 

I am guessing it's failing at the first part (the submit) and can't work out why, all the fields are filled out and if I change the code slightly to

 

  if(isset($_POST['submit'])) {

 

It sends the email automatically and only displays a white page.

 

Any ideas anyone?

  • Author

You have named your submit correctly?

<input type="submit" value="Submit" name="submit" />

 

Yup the submit is right

 

 <input type="submit" value="" id="submit" />
 <input type="reset" value="" id="reset" />

 

You can see the example here

ElanMan is right, your submit button is not named submit, therefore there is no $_POST['submit'] variable for PHP to check.

 

 <input type="submit" value="" id="submit" name="submit" />
 <input type="reset" value="" id="reset" />

  • Author

Got another issue after adding a foreach for the checkboxes...

 

Warning: Invalid argument supplied for foreach() in /home/garethgi/public_html/clients/popup/test.php on line 249

 

Warning: Cannot modify header information - headers already sent by (output started at /home/garethgi/public_html/clients/popup/test.php:249) in /home/garethgi/public_html/clients/popup/test.php on line 258

 

PHP code is now

 

 <?php 

$your_email ='email address here';

$advice = $_POST['advice'];
$name = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$country = $_POST['country'];

if(!isset($_POST['submit'])) {
?>

< html code here >

<?php
 } else {
 // send email

 $to = $your_email;
       $subject="Website Submission";
       $from = $visitor_email;
       $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

       $body =
       "Name: $name\n".
       "Email: $email \n".
       "Phone #: $phone \n".
       "Country of residence: $country \n".
       "Interested in:"; 
        foreach ($advice as $f) {
     echo "$f"; 
    }

       $headers = "From: $from \r\n";
       $headers .= "Reply-To: $visitor_email \r\n";

   mail($to, $subject, $body, $headers); 

   header("Location:test.php");
   exit;
}   
?>

Got another issue after adding a foreach for the checkboxes...

 

Warning: Invalid argument supplied for foreach() in /home/garethgi/public_html/clients/popup/test.php on line 249

 

Warning: Cannot modify header information - headers already sent by (output started at /home/garethgi/public_html/clients/popup/test.php:249) in /home/garethgi/public_html/clients/popup/test.php on line 258

 

PHP code is now

 

 <?php 

$your_email ='email address here';

$advice = $_POST['advice'];
$name = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$country = $_POST['country'];

if(!isset($_POST['submit'])) {
?>

< html code here >

<?php
 } else {
 // send email

 $to = $your_email;
       $subject="Website Submission";
       $from = $visitor_email;
       $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

       $body =
       "Name: $name\n".
       "Email: $email \n".
       "Phone #: $phone \n".
       "Country of residence: $country \n".
       "Interested in:"; 
        foreach ($advice as $f) {
     echo "$f"; 
    }

       $headers = "From: $from \r\n";
       $headers .= "Reply-To: $visitor_email \r\n";

   mail($to, $subject, $body, $headers); 

   header("Location:test.php");
   exit;
}   
?>

 

The $advice variable has to be an array :)

 

http://php.net/manual/en/control-structures.foreach.php

  • Author

The $advice variable has to be an array :)

 

http://php.net/manual/en/control-structures.foreach.php

 

Done that, but can't get the foreach to display in the email...

 

code..

 

  } else {
 // send email

 $to = $your_email;
	$subject="Website Submission";
	$from = $visitor_email;
	$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

	$body =
	"Name: $name\n".
	"Email: $email \n".
	"Phone #: $phone \n".
	"Country of residence: $country \n". 
	"Interested in:  "
	 foreach ($advice as $f) {
     $f;
    };

	$headers = "From: $from \r\n";
	$headers .= "Reply-To: $visitor_email \r\n";

   mail($to, $subject, $body, $headers); 

   header("Location:test.php");
   exit;
}

 

Parse error: syntax error, unexpected T_FOREACH

Done that, but can't get the foreach to display in the email...

 

code..

 

  } else {
 // send email

 $to = $your_email;
	$subject="Website Submission";
	$from = $visitor_email;
	$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

	$body =
	"Name: $name\n".
	"Email: $email \n".
	"Phone #: $phone \n".
	"Country of residence: $country \n". 
	"Interested in:  "
	 foreach ($advice as $f) {
     $f;
    };

	$headers = "From: $from \r\n";
	$headers .= "Reply-To: $visitor_email \r\n";

   mail($to, $subject, $body, $headers); 

   header("Location:test.php");
   exit;
}

 

Parse error: syntax error, unexpected T_FOREACH

 

First off what array are u trying to foreach? and i dont think u can put a foreach statement in a variable i've never done it so could be wrong

  • Author

trying to foreach my array of the checkbox results...

 

e.g. In the email it should read, I am interested in recieving information on: seo, marketing, website design.

 

PHP is not my strong point and I am fine with a basic email form but the multiple checkboxes has stumped me.

trying to foreach my array of the checkbox results...

 

e.g. In the email it should read, I am interested in recieving information on: seo, marketing, website design.

 

PHP is not my strong point and I am fine with a basic email form but the multiple checkboxes has stumped me.

 

U can give this a try

 

 $body =
               "Name: $name\n".
               "Email: $email \n".
               "Phone #: $phone \n".
               "Country of residence: $country \n".
               "Interested in:  ";


               $body .=  "
               foreach ($advice as $f){
            ". $f."
            };
             ";

 

Not sure if it will even work though

  } else {
 // send email

 $to = $your_email;
               $subject="Website Submission";
               $from = $visitor_email;
               $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

               $body =
               "Name: $name\n".
               "Email: $email \n".
               "Phone #: $phone \n".
               "Country of residence: $country \n". 
               "Interested in:  ";

               $body .= implode("\n                *", $advice);

               $headers = "From: $from \r\n";
               $headers .= "Reply-To: $visitor_email \r\n";

   mail($to, $subject, $body, $headers); 

   header("Location:test.php");
   exit;
}

  • Author

  } else {
 // send email

 $to = $your_email;
               $subject="Website Submission";
               $from = $visitor_email;
               $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

               $body =
               "Name: $name\n".
               "Email: $email \n".
               "Phone #: $phone \n".
               "Country of residence: $country \n". 
               "Interested in:  ";

               $body .= implode("\n                *", $advice);

               $headers = "From: $from \r\n";
               $headers .= "Reply-To: $visitor_email \r\n";

   mail($to, $subject, $body, $headers); 

   header("Location:test.php");
   exit;
}

 

Hi Jock, thanks for that but still doesn't do what I need unfortunately.

 

The checkboxes that are ticked need to be displayed, and at the moment it's only displaying one of them, hence why I added a foreach (it works on a tutorial here

 

My full code is:

<?php 

$your_email ='gillman56@googlemail.com';

$advice = array($_POST['advice']);
$name = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$country = $_POST['country'];

if(!isset($_POST['submit'])) {
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sample pop up window</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>

<script type="text/javascript">
$(document).ready(function() {	

	var id = '#dialog';

	//Get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	//Set heigth and width to mask to fill up the whole screen
	$('#mask').css({'width':maskWidth,'height':maskHeight});

	//transition effect		
	$('#mask').fadeIn(1000);	
	$('#mask').fadeTo("slow",0.;	

	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();

	//Set the popup window to center
	$(id).css('top',  winH/2-$(id).height()/2);
	$(id).css('left', winW/2-$(id).width()/2);

	//transition effect
	$(id).fadeIn(2000); 	

//if close button is clicked
$('.window .close').click(function (e) {
	//Cancel the link behavior
	e.preventDefault();

	$('#mask').hide();
	$('.window').hide();
});		

//if mask is clicked
$('#mask').click(function () {
	$(this).hide();
	$('.window').hide();
});		

});

</script>

<style type="text/css">
body {
font-family:verdana;
font-size:12px;
}
img { border:none; }
a {color:#333; text-decoration:none}
a:hover {color:#ccc; text-decoration:none}

p { 
text-align:Center;
width:700px;
margin:0 50px;
}
#mask {
 position:absolute;
 left:0;
 top:0;
 z-index:9000;
 background-color:#000;
 display:none;
}  
#boxes .window {
 position:absolute;
 left:0;
 top:0;
 width:800px;
 height:500px;
 display:none;
 z-index:9999;
 padding:20px;
}
#boxes #dialog {
 width:800px; 
 height:500px;
 padding:10px;
 background:url(backg.png) no-repeat;
}
.close {
float:right;
margin:0 50px 0 0;
}
#boxes ul { list-style-type:none; }
.left {
width:300px;
float:left;
margin:15px 0;
}
.left li {
height:20px;
}
.right {
width:50px;
float:left;
margin:15px 0 15px -40px;
}
.right li {
height:20px;
}
.main {
width:400px;
margin:0 200px;
}
.main li {
float:left;
}
.left2 {
margin:0 25px 0 0;
width:150px;
}
input {
margin:0 0 10px 0;
}
#submit {
margin:15px 25px 0 200px;
border:0;
background:url(send.png) no-repeat;
width:200px;
height:50px;
}
#reset {
margin:15px 0 0 0;
border:0;
width:200px;
height:50px;
background:url(clear.png) no-repeat;
}
h2 {
color:#8E5002;
text-align:center;
padding:20px 0 5px 0;
}
</style>
</head><body>
<h2>test</h2>
<div style="font-size: 10px; color: rgb(204, 204, 204);">Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License.</div>

<div id="boxes">
<div style="top: 199.5px; left: 300px; display: none;" id="dialog" class="window">
<a href="#" class="close"><img src="delete.png" alt="Close window" /></a>
<h2>Sign Up To Our Newsletter</h2>
 <p>Please also tick any of the services you would like more information about (optional)</p>

 <form method="post" action="">
 <ul class="left">
  <li>Search Engine Optimisation (SEO)</li>
  <li>Personal Injury Claims </li>
  <li>Payment Protection (PPI) Claims</li>
  <li>Debt Management Help </li>
  <li>Mortgages & Loans </li>
  <li>Website Design Services</ll>
 </ul>
 <ul class="right">
  <li><input type="checkbox" name="advice" value="seo" /></li>
  <li><input type="checkbox" name="advice" value="pij" /></li>
  <li><input type="checkbox" name="advice" value="ppi" /></li>
  <li><input type="checkbox" name="advice" value="debt" /></li>
  <li><input type="checkbox" name="advice" value="mort" /></li>
  <li><input type="checkbox" name="advice" value="web" /></li>
 </ul>
 <ul class="left">
  <li>Website Marketing/E-mail Campaigns</li>
  <li>E-mail Marketing Services</li>
  <li>Life Insurance</li>
  <li>Pay Day Loans</li>
  <li>Home/House Insurance</li>
  <li>Website Link Building</li>
 </ul>
 <ul class="right">
  <li><input type="checkbox" name="advice" value="mark" /></li>
  <li><input type="checkbox" name="advice" value="email" /></li>
  <li><input type="checkbox" name="advice" value="life" /></li>
  <li><input type="checkbox" name="advice" value="payday" /></li>
  <li><input type="checkbox" name="advice" value="home" /></li>
  <li><input type="checkbox" name="advice" value="links" /></li>
 </ul>
 <br style="clear:both;margin-bottom:25px;" />
 <ul class="main">
  <li class="left2">Name:</li> <li><input type="text" name="fullname" /></li>
 </ul>
 <ul class="main"> 
  <li class="left2">E-mail Address:</li><li><input type="text" name="email" /></li>
 </ul>
 <ul class="main">
  <li class="left2">Telephone:</li><li><input type="text" name="phone" /></li>
 </ul>
 <ul class="main">
  <li class="left2">Country: 
  <select name="country">
   <option value="uk">U.K.</option>
   <option value="us">USA</option>
   <option value="can">Canada</option>
   <option value="other">Other</option>
  </select></li>
 </ul>
  <br style="clear:both;" />
 <input type="submit" value="" id="submit" name="submit" />
 <input type="reset" value="" id="reset" />
 </form>
 <p style="font-size:10px;">If you are interested in receiving further details about the other services listed above we will pass your details onto one of our partners who specialise in that area</p>
</div>
<!-- Mask to cover the whole screen -->
<div style="width: 1478px; height: 602px; display: none; opacity: 0.8;" id="mask"></div>
</div>


<?php
 } else {
 // send email

 $to = $your_email;
  $subject="Website Submission";
  $from = $visitor_email;
  $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

  $body =
   "Name: $name\n".
   "Email: $email \n".
   "Phone #: $phone \n".
   "Country of residence: $country \n". 
   "Interested in:  ";

  $body .= implode("\n                *", $advice);

  $headers = "From: $from \r\n";
  $headers .= "Reply-To: $visitor_email \r\n";

   mail($to, $subject, $body, $headers); 

   header("Location:test.php");
   exit;
}?> 		
</body>
</html>

  • Author

Fixed my issue, I went back to my foreach but did a google and found a way to do what I needed,

 

Thanks everyone for their help

 

Final code was

 

<?php 

if(isset($_POST['submit'])) {

 $your_email ='email@googlemail.com';
 $name = $_POST['fullname'];
 $email = $_POST['email'];
 $phone = $_POST['phone'];
 $country = $_POST['country'];

 foreach($_POST['advice'] as $value)  {
  $check_msg .= "$value, ";
 } 

 // send email

 $to = $your_email;
 $subject="Website Submission";
 $from = $email;
 $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

 $body =
  "Name: $name\n".
  "Email: $email \n".
  "Phone #: $phone \n".
  "Country of residence: $country \n". 
  "Interested in: $check_msg ";

  $headers = "From: $from \r\n";
  $headers .= "Reply-To: $email \r\n";

   mail($to, $subject, $body, $headers); 

   header("Location:test.php");
   exit;

} else {

?>
<html  code >

<?php } ?>
</body>
</html>

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.