我正在开发一个自学项目来学习如何使用Laravel 4.此时,我已经设法让它的大部分工作,但我遇到的问题是我在数据库表中得到2个条目而不是1。
我之前有一个关于重复主键值的错误,它仍然添加了两个条目。我认为这个错误是重复值背后的原因,但我已经修复了这个错误,但这仍然会发生。
如果我的代码非常混乱且无组织,我很抱歉。我将在最后重组它。
这是我的代码(我无法提交图片):
感谢您的帮助!
文件1:views / customer / index.blade.php
@extends('layout')
<?php
$total = "";
$url = "";
function line_totals()
{
$qty = "";
$unit_price = "";
global $total;
$bool = isset($_GET['qty']) || isset($_GET['unit_price']);
if($bool)
{
$qty = $_GET['qty'];
$unit_price = $_GET['unit_price'];
}
$qty = str_replace("\n", '<br>', $qty);
$qty = str_replace("\n", '<br>', $qty);
$unit_price = str_replace("\n", '<br>', $unit_price);
$unit_price = str_replace("\n", '<br>', $unit_price);
$qtyArr = explode('<br>', $qty);
$priceArr = explode('<br>', $unit_price);
$total = array();
for($i = 0; $i < count($qtyArr); $i++)
{
$total[$i] = $qtyArr[$i] * $priceArr[$i];
}
return $total;
}
$total = json_encode(line_totals());
function insert_customer()
{
$customerIsFilled = isset($_GET['date']) || isset($_GET['receipt']) || isset($_GET['name']) || isset($_GET['address']) || isset($_GET['city']) || isset($_GET['state']) || isset($_GET['zip']);
if($customerIsFilled)
{
$id = "";
$name = $_GET['name'];
$address = $_GET['address'];
$city = $_GET['city'];
$state = $_GET['state'];
$zip = $_GET['zip'];
$created_at = date('Y-m-d H:i:s');
$updated_at = date('Y-m-d H:i:s');
$insert_cust = array(
'id' => $id,
'name' => $name,
'address' => $address,
'city' => $city,
'state' => $state,
'zip' => $zip,
'created_at' => $created_at,
'updated_at' => $updated_at
);
DB::table('customer')->insert($insert_cust);
}
else
return;
}
function insert_work()
{
$curId = 0;
$select = DB::select('SELECT * FROM customer');
$ids = array();
for($i = 0; $i < count($select); $i++)
{
if(!empty($select))
{
$ids[$i] = DB::table('customer')->pluck('id');
$curId = max($ids);
}
}
$workIsFilled = isset($_GET['date']) || isset($_GET['receipt']) || isset($_GET['qty']) || isset($_GET['description']) || isset($_GET['unit_price']) || isset($_GET['line_total']) || isset($_GET['created_at']) || isset($_GET['updated_at']);
$line_total = "";
if($workIsFilled)
{
$cust_id = $curId;
$qty = htmlspecialchars($_GET['qty']);
$date = htmlspecialchars($_GET['date']);
$receipt = htmlspecialchars($_GET['receipt']);
$description = htmlspecialchars($_GET['description']);
$unit_price = htmlspecialchars($_GET['unit_price']);
$created_at = date('Y-m-d H:i:s');
$updated_at = date('Y-m-d H:i:s');
$tot = line_totals();
for($i = 0; $i < count($tot); $i++)
{
$line_total .= $tot[$i]."\n";
}
$insert_work = array(
'cust_id' => $cust_id,
'qty' => $qty,
'date' => $date,
'receipt' => $receipt,
'description' => $description,
'unit_price' => $unit_price,
'line_total' => $line_total,
'created_at' => $created_at,
'updated_at' => $updated_at
);
DB::table('work')->insert($insert_work);
}
else
return;
}
// Store HTTP GET request parameters into $url
$get = action('CustomerController@getResults', $url);
?>
@section('content')
<form action="{{ $url }}" method="get">
<table style="margin-left:250px;" cellspacing="5">
<tr>
<td> Name: </td> <td> <input name="name" id="name" type="text" value="John Doe"> </td>
<td> Address: </td> <td> <input name="address" id="address" type="text" value="Street Address"> </td>
<td> City: </td> <td> <input name="city" id="city" type="text" value="City"> </td>
<td> ST: </td> <td> <input name="state" id="state" size="2" maxlength="2" type="text" value="ST"> </td>
<td> Zip: </td> <td> <input name="zip" id="zip" type="text" value="12345"> </td>
</tr>
</table>
<br><br>
<h1 style="text-align:center;">Work Performed</h1>
<table style="margin-left:350px;" cellspacing="5">
<tr>
<td> Date: </td> <td> <input id="date" name="date" type="text" value="14-01-01"></td>
<td> Receipt#: </td> <td> <input name="receipt" value="9000"> </td>
</tr>
</table>
<table style="margin-left:350px;" cellspacing="5">
<tr>
<td> <textarea rows="1" placeholder="Qty" id="qty" name="qty">5<?php echo "\n"; ?>5</textarea> </td>
<td> <textarea rows="1" placeholder="Description" id="description" name="description">Job 1<?php echo "\n"; ?>Job 2</textarea> </td>
<td> <textarea rows="1" placeholder="Unit Price" id = "unit_price" name="unit_price">200<?php echo "\n"; ?>400</textarea> </td>
<td> <textarea rows="1" placeholder="Line Total" id="line_total" name="line_total" disabled></textarea> </td>
</tr>
</table>
<br>
<input style="margin-left:600px;" id="add" type="submit" value="Submit">
</form>
@stop
文件2:views / layout.blade.php
<!doctype html>
<html>
@section('header')
<head>
<title></title>
<style type="text/css">
#subHeading
{
display: block;
position: absolute;
font-style: italic;
font-weight: bold;
font-size: 25pt;
margin-top:-10px;
margin-left: 600px;
}
#qty, #description, #unit_price, #line_total
{
text-align: center;
}
</style>
<link rel="stylesheet" type="text/css" href="css/ui-lightness/jquery-ui.min.css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui.min.js"></script>
</head>
<body>
<span id="subHeading">Invoice</span>
<br>
<hr
<br>
<script type="text/javascript">
$(function(){
$("#date").datepicker({
dateFormat:"y-mm-dd"
});
});
$(document).ready(function(){
if($("#name").val() != "" && $("#address").val() != "" && $("#city").val() != "" && $("#state").val() != "" && $("#zip").val() != "")
{
$("#add").on('click', function()
{
$.ajax({
url: 'customer/index.blade.php',
success: $(function(){
//alert("New Customer!");
<?php insert_customer();?>
})
});
});
}
else if($("#qty").val() != "" && $("#description").val() != "" && $("#unit_price").val() != "")
{
$("#add").on('click', function()
{
$.ajax({
url: 'customer/index.blade.php',
success: $(function(){
//alert("New Work Order Added!");
<?php insert_work(); ?>
})
});
});
}
$("#line_total").hide();
});
/* $.ajax({
url: 'index.blade.php',
success: $(function(data) {
// <?php insert_customer(); ?>
<?php insert_work(); ?>
});*/
</script>
@stop
@yield('header')
@yield('content')
</body>
</html>
答案 0 :(得分:1)
好的,所以你获得双重插入的原因是注释掉的javascript包含没有被注释掉的php函数:
/* $.ajax({
url: 'index.blade.php',
success: $(function(data) {
// <?php insert_customer(); ?>
<?php insert_work(); ?>
});*/
虽然你已经设法在这里注释了javascript,但php仍然会运行。要成功注释掉php,你需要在php标签内工作:
<?php // myFunction(); ?>
<?php /* myFunction(); */ ?>
从查看代码看起来你只需要了解Javascript(客户端语言)与PHP(服务器端语言)的理解,这是没有问题的,这就是学习项目的原因很有价值。以下是您希望找到有用信息的一些信息:
<强> 1。页面加载时将调用页面中的PHP函数调用。这是一个例子:
示例php文件:
<?php
function myFunction(){
echo 'Hello World';
}
?>
<html>
<body>
<?php myFunction(); ?>
</body>
</html>
这会将以下html输出到浏览器:
<html>
<body>
Hello World
</body>
</html>
因此,当您在页面中调用<?php insert_work(); ?>
功能时,它会立即在页面加载时运行,因为它在那里两次,您将重复调用您的函数。< / p>
<强> 2。您无法直接从Javascript调用PHP函数
遗憾的是,您可能无法按照以下方式工作:
<?php
function myPhpFunction(){
// Insert into database on click
}
?>
<html>
<head>
<script>
$('.button').click(function(){
<?php myPhpFunction(); ?>
});
</script>
</head>
...
这里实际发生的是在页面加载时调用的myPhpFunction()
get。你需要的是一个ajax调用,我可以看到你已经附加到你的#add
提交按钮。以下是使用代码的简单示例:
layout.blade.php的简要版本
<html>
<head>
<script>
$("#add").on('click', function()
{
// Grab the form data
var data = $("form").serialize();
// Perform the ajax request
$.ajax({
// Call the route here not the file
url: '/customer',
data: data,
success: $(function(data){
// Add the returned data to the p tag
$("p").html(data);
})
});
});
</script>
</head>
<body>
<form action="{{ $url }}" method="get">
<input name="name" id="name" type="text" value="John Doe">
<textarea rows="1" placeholder="Description" id="description" name="description">
<input id="add" type="submit" value="Submit">
</form>
<p><!-- Your results will load here --></p>
</body>
</html>
您的customer / index.blade.php
的简要版本<?php
function insert_customer(){
// Perform DB insert
echo 'Success! Customer added.';
}
// Call insert_customer()
insert_customer();
?>
在点击您的按钮时,我们获取表单数据并向执行该功能的客户/索引路由发送ajax GET请求,并向我们发送功能已成功的反馈。
很抱歉这么长的答案。希望它有用!