oldwizard Posted June 13, 2014 Share Posted June 13, 2014 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!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/289143-warning-invalid-argument-supplied-for-foreach/ Share on other sites More sharing options...
maxxd Posted June 13, 2014 Share Posted June 13, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289143-warning-invalid-argument-supplied-for-foreach/#findComment-1482610 Share on other sites More sharing options...
oldwizard Posted June 13, 2014 Author Share Posted June 13, 2014 <!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> Quote Link to comment https://forums.phpfreaks.com/topic/289143-warning-invalid-argument-supplied-for-foreach/#findComment-1482611 Share on other sites More sharing options...
maxxd Posted June 13, 2014 Share Posted June 13, 2014 There's no field named 'check' in your form. Which means there'll be no $_POST['check'] for you to use as an array. Quote Link to comment https://forums.phpfreaks.com/topic/289143-warning-invalid-argument-supplied-for-foreach/#findComment-1482613 Share on other sites More sharing options...
oldwizard Posted June 13, 2014 Author Share Posted June 13, 2014 Yes i´ve removed them from the form, how can i fix that? what do i remove from the php code? Sorry php is not my beach, can you give me a hand maxxd? Quote Link to comment https://forums.phpfreaks.com/topic/289143-warning-invalid-argument-supplied-for-foreach/#findComment-1482614 Share on other sites More sharing options...
paddy_fields Posted June 13, 2014 Share Posted June 13, 2014 On a side note, you should really look at using PHPMailer for sending emails in PHP. https://github.com/PHPMailer/PHPMailer Quote Link to comment https://forums.phpfreaks.com/topic/289143-warning-invalid-argument-supplied-for-foreach/#findComment-1482615 Share on other sites More sharing options...
paddy_fields Posted June 13, 2014 Share Posted June 13, 2014 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!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/289143-warning-invalid-argument-supplied-for-foreach/#findComment-1482616 Share on other sites More sharing options...
oldwizard Posted June 13, 2014 Author Share Posted June 13, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289143-warning-invalid-argument-supplied-for-foreach/#findComment-1482617 Share on other sites More sharing options...
Solution oldwizard Posted June 13, 2014 Author Solution Share Posted June 13, 2014 Thanks paddyfields for your help, i´ve downloaded phpmailer and i will look into it. Thanks to maxxd too. Quote Link to comment https://forums.phpfreaks.com/topic/289143-warning-invalid-argument-supplied-for-foreach/#findComment-1482618 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.