Jump to content

Warning: Invalid argument supplied for foreach()


Go to solution Solved by oldwizard,

Recommended Posts

Hi , can somebody help me with this warning?

Warning: Invalid argument supplied for foreach() in /home/wwwdesig/public_html/mailer.php on line 12

It´s just a simple php mailer

Here is the php code:

 

 

 

<?php

if(isset($_POST['submit'])) {
 
$to = "admin@designcarlossimao.com"; 
$subject = "Novo Site";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$option = $_POST['radio'];
$dropdown = $_POST['drop_down'];
 
foreach($_POST['check'] as $value) {
$check_msg .= "Checked: $value\n"; {
// ...
}
}
 
$body = "From: $name_field\n E-Mail: $email_field\n $check_msg Option: $option\n Drop-Down: $dropdown\n Message:\n $message\n";
 
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
 
} else {
echo "blarg!";
}
?>

We'll need to see the HTML form code. Basically, it's saying that $_POST['check'] isn't an array after submission. Post the HTML and please use the '< >' button on the editor to put all the code in code tags - makes it easier to read.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>index</title>

</head><body style="color: rgb(0, 0, 0); background-color: rgb(46, 185, 232);" alink="#000099" link="#000099" vlink="#990099">
<br><br><br><br><table style="width: 300px; text-align: left; margin-left: auto; margin-right: auto;" border="0" cellpadding="10" cellspacing="2"><caption></caption>
<tbody>
<tr>
<td style="background-color: white;">
<table style="text-align: left; width: 100%;" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color: rgb(51, 204, 255); height: 50px; width: 300px;"><big style="font-weight: bold;"><big><span style="color: white;">  Escolha o Seu Novo Site</span></big></big></td>
</tr>
</tbody>
</table>
<form method="post" action="mailer.php"><big><span style="font-weight: bold;">
Nome</span></big>:<br>
<input name="name" size="19" type="text"><br>
<br><big><span style="font-weight: bold;">
E-Mail:</span></big><br>
<input name="email" size="19" type="text"><br>
<br>
<big><bold style="font-weight: bold;">Precisa Logotipo?</bold></big><br>
<input value="yes" name="radio" type="radio"> Sim<br>
<input value="no" name="radio" type="radio"> Não
<br>
<br>
<big><bold style="font-weight: bold;">Escolha um template</bold></big>
<select size="1" name="drop_down"><option>Agua Azul</option><option>Cidade</option><option>Claro</option><option>Elegante</option></select><br>
<big><br style="font-weight: bold;"><span style="font-weight: bold;">Sugestões para o Novo Site:</span></big><br>
<textarea rows="9" name="message" cols="30"></textarea><br>
<br>
<input value="Enviar" name="submit" type="submit">
</form>
</td>
</tr>
</tbody>
</table>
<br>
</body></html>

If you're not using 'check' in the form, then there's no need for the foreach in the PHP as it has no purpose;

 

<?php
if(isset($_POST['submit'])) {
 
$to = "admin@designcarlossimao.com"; 
$subject = "Novo Site";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$option = $_POST['radio'];
$dropdown = $_POST['drop_down'];
 

$body = "From: $name_field\n E-Mail: $email_field\n Option: $option\n Drop-Down: $dropdown\n Message:\n $message\n";
 
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
 
} else {
echo "blarg!";
}
?>

On a side note, you should really look at using PHPMailer for sending emails in PHP.

 

https://github.com/PHPMailer/PHPMailer

 

Thanks paddyfields, but like i said, php is not realy a thing that i´m confortable working with, but i´m always leaning new things here.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.