我创建了动态请求表单,用户可以在其中添加多个表单,并且我有一个名为trackingcode
的字段,它会生成10个随机字符,到目前为止一切正常。
当前,每个请求将生成自己的trackingcode
。现在可以为每个请求生成跟踪代码了吗?
这是一个例子,我添加了3种形式:
当我点击send时,这3个在我的数据库中应该具有相同的跟踪代码。
我正在尝试找到一些有关它的文章,我可以阅读但找不到任何内容。
这是我的插入代码:
<?php
//insert.php
$connect = mysqli_connect("localhost", "root", "", "pcrequest");
if(isset($_POST["reqname"]))
{
$length = 10;
$trackingcode = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
$reqname = $_POST["reqname"];
$date = $_POST["date"];
$empname = $_POST["empname"];
$position = $_POST["position"];
$account = $_POST["account"];
$platform = $_POST["platform"];
$processor = $_POST["processor"];
$ram = $_POST["ram"];
$monitor = $_POST["monitor"];
$phone = $_POST["phone"];
$phonetype = $_POST["phonetype"];
$headset = $_POST["headset"];
$approval = $_POST["approval"];
$status = $_POST["status"];
$query = '';
for($count = 0; $count<count($reqname); $count++)
{
$trackingcode_clean = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
$reqname_clean = mysqli_real_escape_string($connect, $reqname[$count]);
$date_clean = mysqli_real_escape_string($connect, $date[$count]);
$empname_clean = mysqli_real_escape_string($connect, $empname[$count]);
$position_clean = mysqli_real_escape_string($connect, $position[$count]);
$account_clean = mysqli_real_escape_string($connect, $account[$count]);
$platform_clean = mysqli_real_escape_string($connect, $platform[$count]);
$processor_clean = mysqli_real_escape_string($connect, $processor[$count]);
$ram_clean = mysqli_real_escape_string($connect, $ram[$count]);
$monitor_clean = mysqli_real_escape_string($connect, $monitor[$count]);
$phone_clean = mysqli_real_escape_string($connect, $phone[$count]);
$phonetype_clean = mysqli_real_escape_string($connect, $phonetype[$count]);
$headset_clean = mysqli_real_escape_string($connect, $headset[$count]);
$approval_clean = mysqli_real_escape_string($connect, $approval[$count]);
$status_clean = mysqli_real_escape_string($connect, $status[$count]);
if($trackingcode_clean != '' && $reqname_clean != '' && $date_clean != '' && $empname_clean != '' && $position_clean != '' && $account_clean != '' && $platform_clean != '' && $processor_clean != '' && $ram_clean != '' && $monitor_clean != '' && $phone_clean != '' && $phonetype_clean != '' && $headset_clean != '' && $approval_clean != '' && $status_clean != '')
{
$query .= '
INSERT INTO request(trackingcode, reqname, date, empname, position, account, platform, processor, ram, monitor, phone, phonetype, headset, approval, status)
VALUES("'.$trackingcode_clean.'", "'.$reqname_clean.'", "'.$date_clean.'", "'.$empname_clean.'", "'.$position_clean.'", "'.$account_clean.'", "'.$platform_clean.'", "'.$processor_clean.'", "'.$ram_clean.'", "'.$monitor_clean.'", "'.$phone_clean.'", "'.$phonetype_clean.'", "'.$headset_clean.'", "'.$approval_clean.'", "'.$status_clean.'");
';
}
$email_from = "PC Request";//<== update the email address
$email_subject = "PC Request for $account_clean";
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '</body></html>';
$message = '<html><body>';
$message .= "<tr><td>Good morning,</td></tr>";
$message .= "<br>";
$message .= "<br>";
$message .= "<tr><td>Here are the specifications:</td></tr>";
$message .= "<br>";
$message .= "<br>";
$message .= '<table rules="all" style="border-color: #666;" cellpadding="5">';
$message .= "<tr style='background: #FFFFFF;'><td><strong>Tracking Code</strong> </td><td style='color:blue;'>" . $trackingcode_clean . "</td></tr>";
$message .= "<tr style='background: #CDD9FF;'><td><strong>Requested by</strong> </td><td>" . $reqname_clean = mysqli_real_escape_string($connect, $reqname[$count]) . "</td></tr>";
$message .= "<tr style='background: #FFFFFF;'><td><strong>Start Date</strong> </td><td>" . $date_clean = mysqli_real_escape_string($connect, $date[$count]) . "</td></tr>";
$message .= "<tr style='background: #CDD9FF;'><td><strong>Employee Name</strong> </td><td>" . $empname_clean = mysqli_real_escape_string($connect, $empname[$count]) . "</td></tr>";
$message .= "<tr style='background: #FFFFFF;'><td><strong>Position</strong> </td><td>" . $position_clean = mysqli_real_escape_string($connect, $position[$count]) . "</td></tr>";
$message .= "<tr style='background: #CDD9FF;'><td><strong>Account</strong> </td><td>" . $account_clean = mysqli_real_escape_string($connect, $account[$count]) . "</td></tr>";
$message .= "<tr style='background: #FFFFFF;'><td><strong>Platform</strong> </td><td>" . $platform_clean = mysqli_real_escape_string($connect, $platform[$count]) . "</td></tr>";
$message .= "<tr style='background: #CDD9FF;'><td><strong>Processor</strong> </td><td>" . $processor_clean = mysqli_real_escape_string($connect, $processor[$count]) . "</td></tr>";
$message .= "<tr style='background: #FFFFFF;'><td><strong>RAM</strong> </td><td>" . $ram_clean = mysqli_real_escape_string($connect, $ram[$count]) . "</td></tr>";
$message .= "<tr style='background: #CDD9FF;'><td><strong>Monitor</strong> </td><td>" . $monitor_clean = mysqli_real_escape_string($connect, $monitor[$count]) . "</td></tr>";
$message .= "<tr style='background: #FFFFFF;'><td><strong>Phone</strong> </td><td>" . $phone_clean = mysqli_real_escape_string($connect, $phone[$count]) . "</td></tr>";
$message .= "<tr style='background: #CDD9FF;'><td><strong>Phone Type</strong> </td><td>" . $phonetype_clean = mysqli_real_escape_string($connect, $phonetype[$count]) . "</td></tr>";
$message .= "<tr style='background: #FFFFFF;'><td><strong>Headset</strong> </td><td>" . $headset_clean = mysqli_real_escape_string($connect, $headset[$count]) . "</td></tr>";
$message .= "<tr style='background: #CDD9FF;'><td><strong>View Requests</strong> </td><td><a href='teamspan.com/core/pcrequest/messages-admin.php'>Click Here!</a></td></tr>";
$message .= "</table>";
$message .= "</body></html>";
$to = "renzoandredg@gmail.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
//$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$message, $headers);
//done. redirect to thank-you page.
//header('Location: index.php');
}
if($query != '')
{
if(mysqli_multi_query($connect, $query))
{
echo 'Successfuly Sent!';
}
else
{
echo 'Error';
}
}
else
{
echo 'All fields are required!';
}
}
?>
不确定是否有必要,但这是我的JS:
$('#save').click(function(){
var trackingcode = [];
var reqname = [];
var date = [];
var empname = [];
var position = [];
var account = [];
var platform = [];
var processor = [];
var ram = [];
var monitor = [];
var phone = [];
var phonetype = [];
var headset = [];
var approval = [];
var status = [];
$('.trackingcode').each(function(){
trackingcode.push($(this).text());
});
$('.reqname').each(function(){
reqname.push($(this).text());
});
$('.date').each(function(){
date.push($(this).find('input').val());
});
$('.empname').each(function(){
empname.push($(this).text());
});
$('.position').each(function(){
position.push($(this).find('select').val());
});
$('.account').each(function(){
account.push($(this).find('select').val());
});
$('.platform').each(function(){
platform.push($(this).find('select').val());
});
$('.processor').each(function(){
processor.push($(this).find('select').val());
});
$('.ram').each(function(){
ram.push($(this).find('select').val());
});
$('.monitor').each(function(){
monitor.push($(this).find('select').val());
});
$('.phone').each(function(){
phone.push($(this).find('select').val());
});
$('.phonetype').each(function(){
phonetype.push($(this).find('select').val());
});
$('.headset').each(function(){
headset.push($(this).find('select').val());
});
$('.approval').each(function(){
approval.push($(this).text());
});
$('.status').each(function(){
status.push($(this).text());
});
$.ajax({
url:"insert-message.php",
method:"POST",
data:{trackingcode:trackingcode, reqname:reqname, date:date, empname:empname, position:position, account:account, platform:platform, processor:processor, ram:ram, monitor:monitor, phone:phone, phonetype:phonetype, headset:headset, approval:approval, status:status},
success:function(data){
alert(data);
$("td[contentEditable='true']").text("");
for(var i=2; i<= count; i++)
{
$('tr#'+i+'').remove();
}
fetch_item_data();
}
});
});
});
答案 0 :(得分:0)
在$trackingcode_clean
循环中删除了for
,并将值更改为:
VALUES("'.$trackingcode.'", "'.$reqname_clean.'", "'.$date_clean.'", "'.$empname_clean.'", "'.$position_clean.'", "'.$account_clean.'", "'.$platform_clean.'", "'.$processor_clean.'", "'.$ram_clean.'", "'.$monitor_clean.'", "'.$phone_clean.'", "'.$phonetype_clean.'", "'.$headset_clean.'", "'.$approval_clean.'", "'.$status_clean.'");