我已经编写了以下代码并发布了数据,因为我可以在控制台中看到它。但是,它似乎没有添加到数据库中。我认为这可能是我在 process.php 文件中的操作条件:add_new,但我不确定。
在 PHP 上不是一个大人物,所以不确定如何看到它失败的原因,但它成功返回到 AJAX,我认为但没有添加到数据库,所以我认为这可能是我上面所说的。
if($_POST['action'] == "add_new"){
jQuery 和表单:
<script type="text/javascript">
$(document).ready(function(){
$('div.success').hide();
$("form#add_new").submit(function() {
var started = "<?php echo time(); ?>";
var duration = $('#duration').val();
var ticket_price = $('#ticket_price').val();
var win_percentage = $('#win_percentage').val();
var description = $('#description').val();
var available = $('#available').val();
var action = "add_new";
$.ajax({
type: "POST",
url: "/admin/process.php",
data: { started: started, duration: duration, ticket_price: ticket_price, win_percentage: win_percentage, description: description, available: available, action: action },
dataType: 'json',
success: function(data){
$('form#add_new').hide(function(){$('div.success').fadeIn();});
}
});
return false;
});
});
<div class="success">successfully added new ticket</div>
<form id="add_new" method="post" class="form-horizontal">
<fieldset>
<div class="control-group">
<label for="duration" class="control-label">Duration (days)</label>
<div class="controls">
<input type="text" id="duration" class="input-xlarge" value="" />
</div>
</div>
<div class="control-group">
<label for="ticket_price" class="control-label">Ticket Price</label>
<div class="controls">
<input type="text" id="ticket_price" name="ticket_price" class="input-xlarge" value="" />
</div>
</div>
<div class="control-group">
<label for="available" class="control-label">Available to be Won(%)</label>
<div class="controls">
<input type="text" id="available" name="available" class="input-xlarge" value="" />
</div>
</div>
<div class="control-group">
<label for="win_percentage" class="control-label">Percentage of Winners</label>
<div class="controls">
<input type="text" id="win_percentage" name="win_percentage" class="input-xlarge" value="" />
</div>
</div>
<div class="control-group">
<label for="description" class="control-label">Description</label>
<div class="controls">
<textarea rows="4" id="description" name="description" class="input-xlarge"></textarea>
<span class="help-block">Automatic resize</span> </div>
</div>
<div class="control-group">
<div class="controls">
<button class="btn btn-gebo" type="submit">Save changes</button>
</div>
</div>
</fieldset>
</form>
process.php
:
<?php
include "../utils.php";
// Add new raffle ticket
if($_POST['action'] == "add_new"){
$started = $_POST['started'];
$duration = $_POST['duration'];
$ticket_price = $_POST['ticket_price'];
$win_percentage = $_POST['win_percentage'];
$description = mysql_real_escape_string($_POST['description']);
$available = $_POST['available'];
my_query("INSERT INTO " . $db_prefix . " (lotteries(started, duration, ticket_price, win_percentage, description,available) values) VALUES ('$started','$duration','$ticket_price','$win_percentage','$description','$available')");
mysql_query($add_to_shelf) or die(mysql_error());
}
?>