I'm currently building a system using ExpressionEngine that allows users to answer questions in exchange for points, they can then use these points to claim prizes.
I've been writing the functionality to claim a prize, it needs to do the following:
I have the following code which I think is nearly there however I'm struggling with the last bit, the actual success/failure parts. I've used jQuery Ajax:
<script type="text/javascript">
$(function() {
$("#prizeClaim").submit(function() {
var data = "entry_id={entry_id}&member_id={logged_in_member_id}&prize_title={title}&prize_points={prize_points}";
$.ajax({
url: "/prizes/prize_validation/",
data: data,
success: function(html) {
alert(html); // alert the output from the PHP Script
}
});
return false;
});
});
</script>
This code currently just outputs the html of prize_validation in an alert, this is the code used on the validation page so far:
<?php
// All data required made into vars
$entry_id = ($_GET['entry_id']);
$member_id = ($_GET['member_id']);
$prize_title = ($_GET['prize_title']);
$prize_points = ($_GET['prize_points']);
// Select the stock column
$query = ee()->db->query("SELECT field_id_6 FROM exp_channel_data WHERE entry_id = $entry_id");
if ($query->num_rows() > 0)
{
foreach($query->result_array() as $row)
{
// define stock_total
$stock_total = $row['field_id_6'];
// If stock more than 0 go ahead
if($stock_total > 0) {
//remove 1 stock item
ee()->db->query("UPDATE exp_channel_data SET field_id_6 = field_id_6 - 1 WHERE entry_id = $entry_id");
//update users points
$data = array('member_id' => $member_id, 'prize_points' => $prize_points, 'prize_id' => $entry_id);
$sql = ee()->db->insert_string('exp_rmdy_member_prize_data', $data);
ee()->db->query($sql);
}
}
}
?>
{exp:freeform:form form_id="1" return="thanks"}
<input type="hidden" name="name" value="{username}" id="freeform_name" maxlength="150">
<input type="hidden" name="email" value="{email}" id="freeform_email" maxlength="150">
<input type="hidden" name="company" value="{exp:channel:entries channel='company' dynamic='no'}{if {member_code} == {company_code}}{title}: {company_address}{/if}{/exp:channel:entries}" id="freeform_company" maxlength="200">
<input type="hidden" name="prize" value="<?php echo $prize_title ?>" id="freeform_prize" maxlength="150">
<p><input type='submit' name='submit' value='Process order'></p>
{/exp:freeform:form}
This code checks the stock and if the item is in stock it then removes the amount of points from the users total points, this works. However once this has happened I want to submit the Freeform, I'm not 100% sure if this should be within the prize_validation file or in a third location. But after lots of experimentation I'm still not sure how to go about either!
Any hints/tips much appreciated!
Pjacks answer in the above comment helped me fix this.
Do you want to use ajax to submit or a normal form submit. This should submit the form if you put
$("#freeform").submit();
in your call back instead of the alert. Thats assuming the id of your form is called freeform. If you want to do ajax, it's done a bit differently.