switch ($action) error in php webmail - php

I am trying to send emails using php
<HTML>
<HEAD><TITLE>E-mailer</TITLE></HEAD>
<BODY background="bricks.jpg">
<?php
//adv_mailer.php
function mailer_header()
{
?>
<p>
<?php
}
function mailer_footer()
{
?>
<p>
<?php
}
function error_message($msg)
{
mailer_header();
echo "<SCRIPT>alert(\"Error: $msg\");history.go(-1)</SCRIPT>";
mailer_footer();
exit;
}
function user_message($msg)
{
mailer_header();
echo "<SCRIPT>alert(\"$msg\");history.go(-1)</SCRIPT>";
mailer_footer();
exit;
}
function mail_form() {
global $PHP_SELF;
?>
<FORM METHOD="POST" ENCTYPE="MULTIPART/FORM-DATA" ACTION="<?php echo $PHP_SELF ?>">
<INPUT TYPE="HIDDEN" NAME="action" VALUE="send_mail">
<DIV ALIGN="CENTER ">
<TABLE CELLSPACING="2" CELLPADDING="5" WIDTH="79%" BORDER="0">
<TR>
<TH ALIGN="right" WIDTH="16%"><font color="#FFFF00">To</font></TH>
<TD WIDTH="84%" colspan="2"><font color="#FFFF00"><INPUT NAME="mail_to" SIZE="81"></font></TD>
</TR>
<TR>
<TH ALIGN="right" WIDTH="16%"><font color="#FFFF00">Cc</font></TH>
<TD WIDTH="84%" colspan="2"><font color="#FFFF00"><INPUT NAME="mail_cc" SIZE="81"></font></TD>
</TR>
<TR>
<TH ALIGN="right" WIDTH="16%"><font color="#FFFF00">Bcc</font></TH>
<TD WIDTH="84%" colspan="2"><font color="#FFFF00"><INPUT NAME="mail_bcc" SIZE="81"></font></TD>
</TR>
<TR>
<TH ALIGN="right" WIDTH="16%"><font color="#FFFF00">From</font></TH>
<TD WIDTH="84%" colspan="2"><font color="#FFFF00"><INPUT SIZE="81" NAME="mail_from"></font></TD>
</TR>
<TR>
<TH ALIGN="right" WIDTH="16%"><font color="#FFFF00">Reply-to</font></TH>
<TD WIDTH="84%" colspan="2"><font color="#FFFF00"><INPUT SIZE="81" NAME="mail_reply_to"></font></TD>
</TR>
<TR>
<TH ALIGN="right" WIDTH="16%"><font color="#FFFF00">Attachment</font></TH>
<TD WIDTH="84%" colspan="2"><font color="#FFFF00"><INPUT TYPE="FILE" NAME="userfile"></font></TD>
</TR>
<TR>
<TH ALIGN="right" WIDTH="16%"><font color="#FFFF00">Type</font></TH>
<TD WIDTH="23%">
<font color="#FFFF00">
<INPUT TYPE="RADIO" VALUE="text" NAME="mail_type" CHECKED>TEXT</font></TD>
<TD WIDTH="61%">
<font color="#FFFF00">
<INPUT TYPE="RADIO" VALUE="html" NAME="mail_type">HTML</font></TD>
</TR>
<TR>
<TH ALIGN="right" WIDTH="16%"><font color="#FFFF00">Encoding</font></TH>
<TD WIDTH="23%">
<font color="#FFFF00">
<INPUT TYPE="RADIO" VALUE="7bit" NAME="mail_encoding" CHECKED>7BIT</font></TD>
<TD WIDTH="61%">
<font color="#FFFF00">
<INPUT TYPE="RADIO" VALUE="8bit" NAME="mail_encoding">8BIT</font></TD>
</TR>
<TR>
<TH ALIGN="right" WIDTH="16%"><font color="#FFFF00">Character Set</font></TH>
<TD WIDTH="23%">
<font color="#FFFF00">
<INPUT TYPE="RADIO" VALUE="us-ascii" NAME="mail_charset" CHECKED>US-ASCII </font></TD>
<TD WIDTH="61%">
<font color="#FFFF00"><INPUT TYPE="RADIO" VALUE="euc-kr" NAME="mail_charset">EUC-KR</font></TD>
</TR>
<TR>
<TH ALIGN="right" WIDTH="16%"><font color="#FFFF00">Subject</font></TH>
<TD WIDTH="84%" colspan="2"><font color="#FFFF00"><INPUT SIZE="81" NAME="mail_subject"></font></TD>
</TR>
<TR>
<TH ALIGN="right" WIDTH="16%" valign="top"><font color="#FFFF00">Body</font></TH>
<TD WIDTH="84%" colspan="2"><TEXTAREA NAME="mail_body" ROWS="16"
COLS="62"></TEXTAREA></TD>
</TR>
<TR>
<TH WIDTH="100%" COLSPAN="3" ALIGN="CENTER">
<INPUT TYPE="SUBMIT" VALUE="Send" NAME="SUBMIT">
<INPUT TYPE="RESET" VALUE="Reset" NAME="RESET">
</TH>
</TR>
</TABLE>
</DIV>
</FORM>
<?php
}
function send_mail()
{
global $mail_to, $mail_cc, $mail_bcc, $mail_from, $mail_reply_to;
global $mail_body, $mail_subject;
global $userfile, $userfile_type, $userfile_name, $userfile_size;
global $mail_type, $mail_charset, $mail_encoding;
$mail_parts["mail_type"] = $mail_type;
$mail_parts["mail_charset"] = $mail_charset;
$mail_parts["mail_encoding"] = $mail_encoding;
$mail_parts["userfile"] = $userfile;
$mail_parts["userfile_type"] = $userfile_type;
$mail_parts["userfile_name"] = $userfile_name;
$mail_parts["userfile_size"] = $userfile_size;
$mail_parts["mail_to"] = $mail_to;
$mail_parts["mail_from"] = $mail_from;
$mail_parts["mail_reply_to"] = $mail_reply_to;
$mail_parts["mail_cc"] = $mail_cc;
$mail_parts["mail_bcc"] = $mail_bcc;
$mail_parts["mail_subject"] = trim($mail_subject);
$mail_parts["mail_body"] = $mail_body;
if(my_mail($mail_parts))
user_message("Successfully sent an e-mail titled '$mail_subject'.");
else error_message("An unknown error occurred while attempting to
send an e-mail titled '$mail_subject'.");
}
function my_mail($mail_parts)
{
$mail_to = $mail_parts["mail_to"];
$mail_from = $mail_parts["mail_from"];
$mail_reply_to = $mail_parts["mail_reply_to"];
$mail_cc = $mail_parts["mail_cc"];
$mail_bcc = $mail_parts["mail_bcc"];
$mail_subject = $mail_parts["mail_subject"];
$mail_body = $mail_parts["mail_body"];
$mail_type = $mail_parts["mail_type"];
$mail_charset = $mail_parts["mail_charset"];
$mail_encoding = $mail_parts["mail_encoding"];
$userfile = $mail_parts["userfile"];
$userfile_type = $mail_parts["userfile_type"];
$userfile_name = $mail_parts["userfile_name"];
$userfile_size = $mail_parts["userfile_size"];
if(empty($mail_to)) error_message("Empty to field!");
if(empty($mail_subject)) error_message("Empty subject!");
if(empty($mail_body)) error_message("Empty body! ");
$mail_to = str_replace(";", ",", $mail_to);
$mail_headers = '';
if(!empty($mail_from)) $mail_headers .= "From: $mail_from\n";
if(!empty($mail_reply_to)) $mail_headers .= "Reply-to: $mail_reply_to\n";
if(!empty($mail_cc))
$mail_headers .= "Cc: " . str_replace(";", ",", $mail_cc) . "\n";
if(!empty($mail_bcc))
$mail_headers .= "Bcc: " . str_replace(";", ",", $mail_bcc) . "\n";
$mail_subject = stripslashes($mail_subject);
$mail_body = stripslashes($mail_body);
if($userfile_size > 0)
{
$mail_boundary = md5(uniqid(time()));
$mail_headers .= "MIME-Version: 1.0\r\n";
$mail_headers .= "Content-type: multipart/mixed;
boundary=\"$mail_boundary\"\r\n\r\n";
$mail_headers .= "This is a multi-part message in MIME format.\r\n\r\n";
$fp = fopen($userfile, "r");
$file = fread($fp, filesize($userfile));
$file = chunk_split(base64_encode($file));
$new_mail_body = "--$mail_boundary\r\n";
$new_mail_body .= "Content-type:text/plain;charset=$mail_charset\r\n";
$new_mail_body .= "Content-transfer-encoding:$mail_encoding\r\n\r\n";
$new_mail_body .= "$mail_body\r\n";
$new_mail_body .= "--$mail_boundary\r\n";
if(!empty($userfile_type)) $mime_type = $userfile_type;
else $mime_type = "application/octet-stream";
$new_mail_body .= "Content-type:$mime_type;name=$userfile_name\r\n";
$new_mail_body .= "Content-transfer-encoding:base64\r\n\r\n";
$new_mail_body .= $file . "\r\n\r\n";
$new_mail_body .= "--$mail_boundary--";
$mail_body = $new_mail_body;
}
else if($mail_type == 'html')
{
$mail_headers .= "Content-type: text/html; charset=$mail_charset\r\n";
$mail_headers .= "Content-transfer-encoding:$mail_encoding\r\n\r\n";
}
else
{
$mail_headers .= "Content-type: text/plain; charset=$mail_charset\r\n";
$mail_headers .= "Content-transfer-encoding:$mail_encoding\r\n\r\n";
}
return mail($mail_to,$mail_subject,$mail_body,$mail_headers);
}
switch ($action)
{
case "send_mail":
mailer_header();
send_mail();
mailer_footer();
break;
case "mail_form":
mailer_header();
mail_form();
mailer_footer();
break;
default:
mailer_header();
mail_form();
mailer_footer();
break;
}
?>
</BODY>
</HTML>
I keep getting this errors
Notice: Undefined variable: action in C:\wamp\www\5\adv_mail.php on line 226
Notice: Undefined variable: action in C:\wamp\www\5\adv_mail.php on line 231
any help would be appreciated

$action is surely undefined. From your code it appears like you expect that to be the HTML hidden field named action that you've defined in your form. That error is simply saying you are trying to run a switch statement on something that doesn't exist. For that you should initialize it first.
if(isset($_POST['action']))
$action = $_POST['action']; // This is missing
else
die("No action defined");
switch ($action)
{
....
}

Related

Getting values to insert/update in the database according to checked checkbox not working

I'm working with send mail function of my page. To be able to get the values in the table I want to add a checkbox in each row that can tick more than 1 rows. Then once submitted by a button, it will be inserted in the database. I don't know what's wrong with my code.
No error is displaying. But Record is successfully updated! is displayed when I click save button. I forgot to include, I have 2 textboxes and 1 dropdown. I just want to insert and update theses data on the database.
Here's the 2 textboxes and the dropdown list:
<table name=container border=1 align=center>
<td>
<table class='formtb' style='font-size:0.6em;'>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Person in Charge: </td>
<td><textarea rows='4' cols='40' name="pic" value="<?php echo $row2['PersoninCharge']?>" placeholder="Email address only. Use comma(,) for multiple email address."></textarea></td>
<td> </td>
<td></td>
</tr>
<tr>
<td>PIC Comments: </td>
<td><textarea rows='4' cols='40' name='comt' value="<?php echo $row2['PIC_Comments']?>"></textarea></td>
<td> </td>
<td></td>
</tr>
<tr>
<td>Status: </td>
<td><select name="stat">
<option value=""> </option>";
<option value="Open" <?php if($row2['Status']=="Open") echo selected;?>> Open </option>
<option value="Closed" <?php if($row2['Status']=="Closed") echo selected;?>> Closed </option>
</select> </td>
<td></td>
<td></td>
</tr>
<tr>
<td> </td>
<td><input type='submit' name='save_btn' value='♦ SAVE ♦' style='font-size:1.5em;'/></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td></td>
<td></td>
<td> </td>
<td> </td>
</tr>
</table>
</td>
</table>
Here's the table fetched from the database:
<table class ='tb2' border=20 style='font-size:0.6em;' id='result'>
<tr>
<th> </th>
<th>ISSUE TYPE</th>
<th>CREATION DATE</th>
<th>SITE</th>
<th>VENDOR NAME</th>
<th>INVOICE DATE</th>
<th>INVOICE NUMBER</th>
<th>PART NUMBER</th>
<th>PO</th>
<th>RR</th>
<th>CURRENCY</th>
<th>INVOICE AMOUNT</th>
<th> ISSUES </th>
<!--<th>PERSON IN CHARGE</th>
<th>PIC COMMENTS</th>
<th>STATUS</th>-->
</tr>
<?php
$invtxt = $_POST['InvNumbTxt'];
$conn=dbConnect();
$sql="SELECT * FROM invalid_invoice";
$stmt=$conn->prepare($sql);
$stmt->execute();
if(!isset($_POST['search_btn'])) {
while($row =$stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<form name='update' method='POST' autocomplete='off'>";
echo "<tr class=output2>";
echo "<td><input type='checkbox' name='checkb[]' value='".$row['ID']."'></td>";
echo "<td>".$row['Issue_Type']."</td>";
echo "<td>".$row['Creation_Date']."</td>";
echo "<td>".$row['Site']."</td>";
echo "<td>".$row['Vendor_Name']."</td>";
echo "<td>".$row['Invoice_Date']."</td>";
echo "<td>".$row['Invoice_Number']."</td>";
echo "<td>".$row['Part_Number']."</td>";
echo "<td>".$row['PO']."</td>";
echo "<td>".$row['RR']."</td>";
echo "<td>".$row['Currency']."</td>";
echo "<td>".$row['Invoice_Amount']."</td>";
echo "<td>".$row['Issues']."</td>";
echo "</tr>";
echo "</form>";
}
}
$conn=null;
</table>
Here's the save button:
if(isset($_POST['save_btn'])) {
/*if(empty($_POST['checkb[]'])) {
echo "<script language='javascript'>alert('No data saved!')</script>";}
else*/ if(empty($_POST['stat'])){
echo "<script language='javascript'>alert('No data saved!')</script>"; }
else if (!filter_var($pic, FILTER_VALIDATE_EMAIL)) {
echo "<script language='javascript'>alert('Invalid email format. Please re-enter valid email only!')</script>"; }
else {
try {
$concat = implode(",",$_POST['checkb']);
$deconcat = explode(",",$concat);
foreach($deconcat as $ndeconcat)
{
$sql2="UPDATE invalid_invoice SET
UpdateBy =:updateby,
UpdateDateTime=NOW(),
PersoninCharge=:pic,
PIC_Comments=:comt,
Status=:stat
WHERE ID =:id";
$conn=dbConnect();
$stmt2=$conn->prepare($sql2);
$stmt2->bindParam(':updateby', $_SESSION['login_user'], PDO::PARAM_STR);
$stmt2->bindParam(':pic', $_POST['pic'], PDO::PARAM_STR);
$stmt2->bindParam(':comt', $_POST['comt'], PDO::PARAM_STR);
$stmt2->bindParam(':stat', $_POST['stat'], PDO::PARAM_STR);
$stmt2->bindParam(':id', $_POST['idtxt'], PDO::PARAM_INT);
$stmt2->execute();
//echo a message to say the UPDATE succeeded
echo $stmt2->rowCount() . " records UPDATED successfully";
$mess2=$stmt2->execute();
if($mess2) {
echo "<script language='javascript'>alert('Record is successfully updated!')</script>";
echo "<meta http-equiv='REFRESH' content='0;url='ap.Invalid.Invoice.Tracking.php'>";
}
else {
echo "<script language='javascript'>alert('Unable to update record. Please try again!')</script>";
}
$conn = null;
//Insert Email:
$sql6 = "SELECT * FROM invalid_invoice WHERE ID=:id6";
$conn = dbConnect();
$stmt6 = $conn->prepare($sql6);
$stmt6->bindParam(':id6', $_POST['idtxt'], PDO::PARAM_INT);
$stmt6->execute();
$data = $stmt6->fetchAll(PDO::FETCH_ASSOC);
$conn=null;
foreach ($data as $row6) {
$invnumb=$row6['Invoice_Number'];
$partnumb=$row6['Part_Number'];
$issue=$row6['Issues'];
$pic=$row6['PIC_Comments'];
$emailadd= $row6['PersoninCharge'];
$issuetype=$row6['Issue_Type'];
$createdate=$row6['Creation_Date'];
$site=$row6['Site'];
$vendor=$row6['Vendor_Name'];
$invdate=$row6['Invoice_Date'];
$po=$row6['PO'];
$rr=$row6['RR'];
$currency=$row6['Currency'];
$invamount=$row6['Invoice_Amount'];
$stat=$row6['Status'];
if($row6['Status']=="Open") {
$message = "<html><b>Issue Type: {$issuetype} </b><br><br>";
$message .= "<b>Creation Date: {$createdate} </b><br><br>";
$message .= "<b>Site: {$site} </b><br><br>";
$message .= "<b>Vendor Name: {$vendor} </b><br><br>";
$message .= "<b>Invoice Date: {$invdate} </b><br><br>";
$message .= "<b>Invoice Number: {$invnumb} </b><br><br>";
$message .= "<b>Part Number:</b><br>{$partnumb}<br><br>";
$message .= "<b>PO: {$po} </b><br><br>";
$message .= "<b>RR: {$rr} </b><br><br>";
$message .= "<b>Currency: {$currency} </b><br><br>";
$message .= "<b>Invoice Amount: {$invamount} </b><br><br>";
$message .= "<b>Issues:</b><br>{$issue}<br>";
$message .= "<b>Status: {$stat} </b><br><br>";
$message .= "<b>{$pic}<b><br>";
$message .= "</html>";
if(!empty($emailadd)) {
dbInsertEmailMessage($emailadd, "Invoice Number: {$invnumb} - {$issue}.", $message);
echo "alert('Email sent to {$emailadd}')";
}
}
}
}
function dbInsertEmailMessage($send_to, $subject, $message) {
$sql7 = "INSERT INTO email_queue (send_to, subject, message) VALUES (:send_to, :subject, :message)";
$conn = dbConnect();
$stmt7 = $conn->prepare($sql7);
$stmt7->bindParam(':send_to', $send_to);
$stmt7->bindParam(':subject', $subject);
$stmt7->bindParam(':message', $message);
$stmt7->execute();
$conn=null;
}
}
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
}
?>
I've tried to use this, but it didn't work. (Actually I don't know how it works.)
$(document).ready(function() {
$("input[name='checkb[]']").click(function() {
var output = "";
$("input:checked").each(function() {
output += $(this).val() + ",";
});
$("input[name='idtxt']").val(output.trim());
});
});

Array to string conversion notice when using PHP to email dynamic data from html form.

I have built an HTML form that has buttons to add rows for multiple inputs. I want to email all the rows with the input data no matter how many rows are added. So far I get the email but its only 1 row of data and all the input fields are saying "Array" and I receive the notice of "Array to string conversion on line 34" & "Array to string conversion on line 41"
Is this even possible?
HTML Form Code:
<form action="Email.php" method="POST">
<table id="2HL" name="Pass">
<tbody>
<tr>
<td><font style="color: #b22222; font-size:120%">Date</font></td>
<td><input type="datetime" name="Date"></td>
</tr>
<tr>
<td><font style="color: #b22222; font-size:120%">Team Lead</font></td>
<td><input type="text" name="Team_Lead"></td>
</tr>
<tr>
<td><font style="color: #00008b; font-size:120%">End of Shift Information:</font></td>
</tr>
<tr>
<table id="Cellinfo">
<tbody>
<tr>
<td Type="text" name="Cell"><font style="color: #b22222; font-size:100%">CELL</font></td>
<td Type="text" name="Goal"><font style="color: #b22222; font-size:100%">GOAL</font></td>
<td Type="text" name="Comp"><font style="color: #b22222; font-size:100%">COMP</font></td>
<td Type="text" name="LTD"><font style="color: #b22222; font-size:100%">LTD</font></td>
<td Type="text" name="WET_SKU"><font style="color: #b22222; font-size:100%">WET SKU</font></td>
</tr>
<tr id="clone">
<td><input type="text" name="Cell[]"></td>
<td><input type="text" name="Goal[]"></td>
<td><input type="text" name="Comp[]"></td>
<td><input type="text" name="LTD[]"></td>
<td><input type="text" name="WET_SKU[]"></td>
</tr>
</tbody>
</table>
<input type="button" onclick="cloneRow()" value="ADD ROW" /><br>
</tr>
<tr>
<td><font style="color: #00008b; font-size:120%">Impediment Notes:</font></td>
</tr>
<tr>
<table id="impediment">
<tbody>
<tr>
<td Type="text" name="Cell0"><font style="color: #b22222; font-size:100%">CELL</font></td>
<td Type="text" name="SKU0"><font style="color: #b22222; font-size:100%">SKU</font></td>
<td Type="text" name="Part0"><font style="color: #b22222; font-size:100%">PART #</font></td>
<td Type="text" name="Impediment0"><font style="color: #b22222; font-size:100%">IMPEDIMENT TYPE</font></td>
<td Type="text" name="QTYadj0"><font style="color: #b22222; font-size:100%">QTY ADJUSTED</font></td>
<td Type="text" name="Partloc0"><font style="color: #b22222; font-size:100%">PART LOCATION</font></td>
<td Type="text" name="Comments0"><font style="color: #b22222; font-size:100%">TEAM LEAD COMMENTS</font></td>
</tr>
<tr id="clone1">
<td><input type="text" name="Cella[]"></td>
<td><input type="text" name="SKU[]"></td>
<td><input type="text" name="Part[]"></td>
<td><input type="text" name="Impediment[]"></td>
<td><input type="text" name="QTYadj[]"></td>
<td><input type="text" name="Partloc[]"></td>
<td><input type="text" name="Comments[]" size="75"></td>
</tr>
</tbody>
</table>
<input type="button" onclick="cloneimpediment()" value="ADD ROW" /><br>
</tr>
</tbody>
</table>
<p>
<input type="submit" value="Email Form">
</P>
</form>
Java Code to Add Rows:
<script type="text/javascript">
function cloneRow()
{
var row = document.getElementById("clone"); // find row to copy
var table = document.getElementById("Cellinfo"); // find table to append to
var clone = row.cloneNode(true); // copy children too
clone.id = "clone"; // change id or other attributes/contents
table.appendChild(clone); // add new row to end of table
}
function cloneimpediment()
{
var row = document.getElementById("clone1"); // find row to copy
var table = document.getElementById("impediment"); // find table to append to
var clone = row.cloneNode(true); // copy children too
clone.id = "clone1"; // change id or other attributes/contents
table.appendChild(clone); // add new row to end of table
}
</script>
PHP Code Here:
<?php foreach($_POST as $key => $value){
$Date = $_POST['Date'];
$Team_Lead = $_POST['Team_Lead'];
$Cell = $_POST['Cell'];
$Goal = $_POST['Goal'];
$Comp = $_POST['Comp'];
$LTD = $_POST['LTD'];
$WET_SKU = $_POST['WET_SKU'];
$Cella = $_POST['Cella'];
$SKU = $_POST['SKU'];
$Part = $_POST['Part'];
$Impediment = $_POST['Impediment'];
$QTYadj = $_POST['QTYadj'];
$Partloc = $_POST['Partloc'];
$Comments = $_POST['Comments'];
}
$email_from = 'TestTeamLead#moen.com';
$email_subject = "Pass Down Test";
$email_body = "<html><body>";
$email_body .= '<table border="1">';
$email_body .= "<tr><td>Date</td><td>$Date</td></tr>";
$email_body .= "<tr><td>Team Lead</td><td>$Team_Lead</td></tr>";
$email_body .= "</table>";
$email_body .= "<br>";
$email_body .= "END OF SHIFT INFORMATION:";
$email_body .= "<br>";
$email_body .= '<table border="1">';
$email_body .= '<tr><td>Cell</td><td>Goal</td><td>Comp</td><td>LTD</td><td>WET SKU</td></tr>';
$email_body .= "<tr><td>$Cell</td><td>$Goal</td><td>$Comp</td><td>$LTD</td><td>$WET_SKU</td></tr>";
$email_body .= "</table>";
$email_body .= "<br>";
$email_body .= "IMPEDIMENT NOTES:";
$email_body .= "<br>";
$email_body .= '<table border="1">';
$email_body .= '<tr><td>Cell</td><td>SKU</td><td>PART #</td><td>ImpedimentType</td><td>QTY Adjusted</td><td>Part Location</td><td>TEAM LEAD Comments</td></tr>';
$email_body .= "<tr><td>$Cella</td><td>$SKU</td><td>$Part</td><td>$Impediment</td><td>$QTYadj</td><td>$Partloc</td><td>$Comments</td></tr>";
$email_body .= "</table>";
?>
Well, I guess here foreach($_POST as $key => $value) you want to iterate through all of the HTML table rows. But the thing is that you have on server exactly what you've specified on client.
Try print_r($_POST), so you'll have an idea of the data that comes to server. You should iterate through a random key of yours or rewrite the client code to get the php script in accordance.
Code on line 31 and 41 are arrays, you should iterate in for loop to show them in html, that is why you get array notice.why are your input field name atribute set as array []?

Recaptcha into custom PHP form

I am trying to add recaptcha into my custom PHP form and am confused about how to do so. Recaptcha requires adding recaptchalib.php into the form, but if I add verify.php then my form will not process, as I am using my PHP file to process my form.
<form method="POST" action="process.php" id="form-ok">
The documentation is a bit confusing. My question is, what do I need to do to process two actions?
Any idea how can I get this working?
Process.php
<?php
$redirectTo = '/thankyou.html';
$subject = 'New message from site'; // Email SUBJECT field
$receive = array(
'example#example.com'
);
if($_POST['email_check'] == '') {
if (isset($_POST['first_name'])){
$message = '<table width="100%" border="0" cellspacing="0" cellpadding="8" style="border:1px solid #f3f3f3">
<tr>
<td colspan="3" height="30" style="font-size:20px"><strong>' . $subject . '</strong></td>
</tr>
<tr>
<td width="100" bgcolor="#f3f3f3"><strong>First Name: </strong></td>
<td width="14" height="30" bgcolor="#f3f3f3"> </td>
<td width="305" bgcolor="#f3f3f3">' . $_POST ['first_name'] . '</td>
</tr>
<tr>
<td><strong>Last Name: </strong></td>
<td width="14" height="30"> </td>
<td>' . $_POST ['last_name'] . '</td>
</tr>
<tr>
<td bgcolor="#f3f3f3"><strong>Email: </strong></td>
<td bgcolor="#f3f3f3" width="14" height="30"> </td>
<td bgcolor="#f3f3f3">' . $_POST ['email'] . '</td>
</tr>
<tr>
<td><strong>Phone Number: </strong></td>
<td width="14" height="30"> </td>
<td>' . $_POST ['phone'] . '</td>
</tr>
<tr>
<td bgcolor="#f3f3f3"><strong>Check: </strong></td>
<td bgcolor="#f3f3f3" width="14" height="30"> </td>
<td bgcolor="#f3f3f3">';
foreach($_POST['role'] as $value)
{
$message.=$value.'<br>';
}
$message.='</td>
</tr>
<tr>
<td><strong>Message: </strong></td>
<td width="14" height="30"> </td>
<td>' . $_POST ['message'] . '</td>
</tr>
<tr>
<td><strong>Referer:</strong></td>
<td width="14" height="30"> </td>
<td>' . $_SERVER ['HTTP_REFERER'] . '</td>
</tr>
<tr>
</table>';
for ($i = 0; $i < count($receive); $i++){
$to = $receive[$i];
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers .= 'From: ' . $_POST['email'] . "\n" . 'Reply-To: ' . $_POST['email'] . "\n";
mail($to, $subject, $message,$headers);
}
header('Location: '.$redirectTo);
}
}
else{
header('Location:'.$_SERVER['HTTP_REFERER']); die();
}
?>
Add your recaptchalib.php in your dir.
YOUR PROCESS.PHP:
require_once "../recaptchalib.php"; // where you store recaptchalib.php
$secret = "6Le2g_sSxxxxxxxxxxxxxxxxxxxxxxxx"; //your secret key
$resp = null;
$error = null;
$reCaptcha = new ReCaptcha($secret);
if ($_POST["g-recaptcha-response"]) {
$resp = $reCaptcha->verifyResponse($_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]);
}
if (isset($_POST['cmdlogin'])){
if ($resp != null && $resp->success) {
echo "<script>alert('Success Verifying Recaptcha!');</script>";
echo "<meta http-equiv='refresh' content='0; url=login.php'>";
exit();
}
<form method="post" action="process.php">
.....other codes---
<div class="g-recaptcha" data-
sitekey="6Le2g_sSxxxxxxxxxxxxxxxxxxxxxxxx">
</div>
.....other codes---
</form>
Full Tutorial, check here:
https://github.com/google/ReCAPTCHA/tree/master/php
Client Side (How to make the CAPTCHA image show up)
<form method="post" action="process.php">
<?php
require_once('recaptchalib.php');
$publickey = "YOUR_PUBLIC_KEY"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<input type="submit" />
</form><br>
<!-- more of your HTML content -->
Server Side
The following code should be placed at the top of the process.php file:
<?php
require_once('recaptchalib.php');
$privatekey = "YOUR_PRIVATE_KEY";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
}
?>

PHP conditionals for $message in mail

I know there is a simpler way to do this. What I'm trying to do is group fields by number and use isset to test if $_POST['item#'] value is empty. If that item# is not empty, I want to send corresponding color, quantity, and price. If there is more than one item submitted, I want to send multiples.
Here are my post variables:
$item1 = $_POST['item1'];
$color1 = $_POST['color1'];
$quantity1 = $_POST['quantity1'];
$price1 = $_POST['price1'];
$item2 = $_POST['item2'];
$color2 = $_POST['color2'];
$quantity2 = $_POST['quantity2'];
$price2 = $_POST['price2'];
$item3 = $_POST['item3'];
$color3 = $_POST['color3'];
$quantity3 = $_POST['quantity3'];
$price3 = $_POST['price3'];
$item4 = $_POST['item4'];
$color4 = $_POST['color4'];
$quantity4 = $_POST['quantity4'];
$price4 = $_POST['price4'];
$item5 = $_POST['item5'];
$color5 = $_POST['color5'];
$quantity5 = $_POST['quantity5'];
$price5 = $_POST['price5'];
$item6 = $_POST['item6'];
$color6 = $_POST['color6'];
$quantity6 = $_POST['quantity6'];
$price6 = $_POST['price6'];
$item7 = $_POST['item7'];
$color7 = $_POST['color7'];
$quantity7 = $_POST['quantity7'];
$price7 = $_POST['price7'];
$item8 = $_POST['item8'];
$color8 = $_POST['color8'];
$quantity8 = $_POST['quantity8'];
$price8 = $_POST['price8'];
I'm using isset to test if $_POST[] values are empty:
if( isset($_POST['item1']) )
{
$message = 'Item: '.$item1." \nColor: ".$color1." \nQuantity: ".$quantity1." \nPrice: ".$price1;
}
if( isset($_POST['item2']) )
{
$message = 'Item: '.$item2." \nColor: ".$color2." \nQuantity: ".$quantity2." \nPrice: ".$price2;
}
if( isset($_POST['item3']) )
{
$message = 'Item: '.$item3." \nColor: ".$color3." \nQuantity: ".$quantity3." \nPrice: ".$price3;
}
if( isset($_POST['item4']) )
{
$message = 'Item: '.$item4." \nColor: ".$color4." \nQuantity: ".$quantity4." \nPrice: ".$price4;
}
if( isset($_POST['item5']) )
{
$message = 'Item: '.$item5." \nColor: ".$color5." \nQuantity: ".$quantity5." \nPrice: ".$price5;
}
if( isset($_POST['item6']) )
{
$message = 'Item: '.$item6." \nColor: ".$color6." \nQuantity: ".$quantity6." \nPrice: ".$price6;
}
if( isset($_POST['item7']) )
{
$message = 'Item: '.$item7." \nColor: ".$color6." \nQuantity: ".$quantity7." \nPrice: ".$price7;
}
if( isset($_POST['item7']) )
{
$message = 'Item: '.$item8." \nColor: ".$color7." \nQuantity: ".$quantity8." \nPrice: ".$price8;
}
$items = $_POST['item'];
for ($i = 0; $i < count($items); $i++) {
if ($items[$i])){ //not necessarily the best way to check if it has a value
//do what you want with $items[$i], $color[$i], $quantity[$i], and $price[$]
}
}
For your HTML form inputs use:
<input type="text" name="item[]" />
Here is the requested HTML. I'm just learning so I'm sure there is a more efficient way to name inputs. I'm trying to group item-color-quantity-price, so that when the form is emailed, the relevant data is in a paragraph or table for each item.
<td align="center"><p><span>Item Name</span><br><input id="item1" type="text" name="item1"></p></td>
<td align="center"><p><span>Color</span><br><input id="color1" type="text" name="color1"></td>
<td align="center"><p><span>Quantity</span><br><input id="quantity1" type="text" name="quantity1"></td>
<td align="center"><p><span>Wholesale Price</span><br><input id="price1" type="text" name="price1"></td>
</tr>
<tr>
<td align="center"><p><input id="item2" type="text" name="item2"></p></td>
<td align="center"><p><input id="color2" type="text" name="color2"></p></td>
<td align="center"><p><input id="quantity2" type="text" name="quantity2"></p></td>
<td align="center"><p><input id="price2" type="text" name="price2"></p></td>
</tr>
<tr>
<td align="center"><p><input id="item3" type="text" name="item3"></p></td>
<td align="center"><p><input id="color3" type="text" name="color3"></p></td>
<td align="center"><p><input id="quantity3" type="text" name="quantity3"></p></td>
<td align="center"><p><input id="price3" type="text" name="price3"></p></td>
</tr>
<tr>
<td align="center"><p><input id="item4" type="text" name="item4"></p></td>
<td align="center"><p><input id="color4" type="text" name="color4"></p></td>
<td align="center"><p><input id="quantity4" type="text" name="quantity4"></p></td>
<td align="center"><p><input id="price4" type="text" name="price4"></p></td>
</tr>
<tr>
<td align="center"><p><input id="item5" type="text" name="item5"></p></td>
<td align="center"><p><input id="color5" type="text" name="color5"></p></td>
<td align="center"><p><input id="quantity5" type="text" name="quantity5"></p></td>
<td align="center"><p><input id="price5" type="text" name="price5"></p></td>
</tr>
<tr>
<td align="center"><p><input id="item6" type="text" name="item6"></p></td>
<td align="center"><p><input id="color6" type="text" name="color6"></p></td>
<td align="center"><p><input id="quantity6" type="text" name="quantity6"></p></td>
<td align="center"><p><input id="price6" type="text" name="price6"></p></td>
</tr>
<tr>
<td align="center"><p><input id="item7" type="text" name="item7"></p></td>
<td align="center"><input id="color7" type="text" name="color7"></p></td>
<td align="center"><input id="quantity7" type="text" name="quantity7"></p></td>
<td align="center"><p><input id="price7" type="text" name="price7"></p></td>
</tr>
<tr>
<td align="center"><p><input id="item8" type="text" name="item8"></p></td>
<td align="center"><p><input id="color8" type="text" name="color8"></p></td>
<td align="center"><p><input id="quantity8" type="text" name="quantity8"></p></td>
<td align="center"><p><input id="price8"type="text" name="price8"></p></td>
</tr>
<tr>
<td align="center"><p><input id="item9" type="text" name="item9"></p></td>
<td align="center"><p><input id="color9" type="text" name="color9"></p></td>
<td align="center"><p><input id="quantity9" type="text" name="quantity9"></p></td>
<td align="center"><p><input id="price9" type="text" name="price9"></p></td>
</tr>
I converted all my input names to arrays and I have working loop. However, I need to format the foreach to produce an HTML email with table. It was working perfectly when echo-ing the data but once I formatted it to send as $message, it only sends the last $item.
Here is the foreach loop that works perfectly:
foreach ( $_POST['item'] as $items) {
{
echo '<table>';
echo '<tr>';
echo ' <td>', $items['name'], '</td>';
echo ' <td>', $items['color'], '</td>';
echo ' <td>', $items['size'], '</td>';
echo ' <td>', $items['quantity'], '</td>';
echo ' <td>', $items['price'], '</td>';
echo '</tr>';
}
echo '</table>';
}
HTMl email using the PHP mail function:
foreach ( $_POST['item'] as $items) {
$message = "
<html>
<body>
<table>
<tr>
<th>Name</th>
<th>Color</th>
<th>Size</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<tr>
<td>" . $items['name'] . "</td>
<td>" . $items['color'] . "</td>
<td>" . $items['size'] . "</td>
<td>" . $items['quantity'] . "</td>
<td>" . $items['price'] . "</td>
</tr>
</table>
</body>
</html>
";
}

Radio Button not working [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Looking at some tutorials I have added the radio button but for some reasons it is not working. When i submit the form the values are not entered into the database
Here is my code
case 'add':
if( $_POST['addbtn'] == 'Save' )
{
$name = '';
if(isset($_REQUEST['name']))
{
$name = $_REQUEST['name'];
}
$imageName = '';
$field = 'banner_images';
if($_FILES[$field ]['error'] == 0)
{
if($_FILES[$field]['tmp_name'] != '')
{
$uploaddir = "../inner_page_banner_images/";
$banner_images = $_FILES[$field]['name'];
$tag = "";
$Twidth = 980;
$Theight = 420;
#move_uploaded_file($_FILES[$field]['tmp_name'],$uploaddir.$_FILES[$field]['name']);
}
$site = '';
if(isset($_REQUEST['site']) == 'hino')
{
$site = $_REQUEST['checked'];
}
elseif(isset($_REQUEST['site']) == 'storm')
{
$site = $_REQUEST['checked'];
}
}
//--------------------------------------------------------------------------------------------------------
$Query = "INSERT INTO inner_banner SET ";
$Query .= "name = '".$name."',";
$Query .= "banner_images = '".$banner_images."'";
$Query .= "site = '".$site."'";
//echo $Query; die();
mysql_query($Query);
$_SESSION['msg'] = " New banner image uploaded successfully ";
#header("Location: inner_banner.php? module=inner_banner&action=view");
die();
}
include "add_banner.php";
break;
My Html Code
<form method="post" name="frm1" id="frm1" action="" autocomplete="off" enctype="multipart/form-data" onsubmit="return ValidateForm(this);" >
<table cellspacing="0" cellpadding="3" border="0" align="center" width="100%">
<tr>
<td width="18%">Banner Title</td>
<td width="1%">:</td>
<td><input type="text" name="name" id="name" size="40" value="" />
<span class="required">*</span> </td>
</tr>
<tr>
<td colspan="3" style=" background-color: #FFFFFF;"> </td>
</tr>
<tr>
<td valign="top">Banner Image </td>
<td valign="top">:</td>
<td><input type="file" name="banner_images" id="banner_images" size="20" />
</td>
</tr>
<tr>
<td colspan="3" style=" background-color: #FFFFFF;"> </td>
</tr>
<tr>
<td valign="top">Select:</td>
<td valign="top">:</td>
<td><input type="radio" value="hino" name="site" id="site" />
HINO
<input type="radio" value="storm" name="site" id="site" />
Storm
<span class="required">*</span>
</td>
</tr>
<tr>
<td colspan="3" style=" background-color: #FFFFFF;"> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="addbtn" value="Save" />
<input type='button' value='Cancel' onclick="document.location.href='inner_banner.php?module=inner_banner'" />
<input type="hidden" name="Validation" id="Validation" value="
Field=name|Alias=Banner Title|Validate=BLANK^
Field=banner_images|Alias=Banner Image|Validate=BLANK" />
can somebosy pls look into it why the values are not getting entered in the database
This line is missing a comma:
$Query .= "banner_images = '".$banner_images."'";
should be
$Query .= "banner_images = '".$banner_images."',";
As a sidenote, you are vulnerable to sql injection with your code, you might want to use prepared statements or at least mysql_real_escape_string
Shouldn't this:
if(isset($_REQUEST['site']) == 'hino')
{
$site = $_REQUEST['checked'];
}
elseif(isset($_REQUEST['site']) == 'storm')
{
$site = $_REQUEST['checked'];
}
be:
if(isset($_REQUEST['site']) == 'hino')
{
$site = $_REQUEST['site'];
}
elseif(isset($_REQUEST['site']) == 'storm')
{
$site = $_REQUEST['site'];
}
if so, a better way to code this is:
if(isset($_REQUEST['site']) && ($_REQUEST['site'] == 'hino' || isset($_REQUEST['site']) == 'storm') {
$site = $_REQUEST['site'];
}
Also:
$Query .= "name = '".$name."',";
$Query .= "banner_images = '".$banner_images."',";
$Query .= "site = '".$site."'";

Categories