I have coded the following and its posting the data as I can see it in console. However, it does not seem to add to the database. I think it could be my condition in the process.php file for action: add_new, but I'm not sure.
Not a huge person on PHP so unsure how to see why it's failing but it's returning successfully to the AJAX, I think but not adding to database, so I assume it might be what I said above.
if($_POST['action'] == "add_new"){
jQuery and Form:
<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());
}
?>