I have three files: submit.php
(a page), validation.php
, script.php
I am trying to use the fields as a front end submission area via the wp_insert_post()
function.
However, I don't know exactly where to put the function, as where I try either is before the validation.php
file or if I put it in that file it causes an error since it can't pass wordpress functions.
I have tried playing with the success
in the $.ajax
but since the die()
functions all result in it being successful can't trigger a create post.
Does anyone have a better solution?
In the submit.php
I have a form:
<div class="" id="response"></div>
<form id="form_ticket_submit" method="post">
<label for="form_ticket_subject">Subject of issue</label>
<input id="form_ticket_subject" name="form_ticket_subject" type="text">
<label for="form_ticket_content">Post</label>
<textarea id="form_ticket_content" name="form_ticket_content"></textarea>
<label for="form_ticket_tax_stage">Select Stage</label>
<select id="form_ticket_tax_stage" name="form_ticket_tax_stage">
<option value="">Select one</option>
<?php foreach ($form_ticket_tax_cat_stage as $form_cat_stage) { echo '<option value="' . $form_cat_stage->slug . '">'. $form_cat_stage->name . '</option>'; } ?>
</select>
<input hidden="hidden" id="form_ticket_meta_date" name="form_ticket_meta_date" type="text" readonly="readonly" value="<?php echo get_date_from_gmt( date( 'Y-m-d H:i:s' ), 'jS \of F, Y H:i:s' ); ?>">
<input hidden="hidden" id="form_ticket_meta_user_name" name="form_ticket_meta_user_name" type="text" readonly="readonly" value="<?php echo $form_ticket_current_user_name; ?>">
<input hidden="hidden" id="form_ticket_meta_user_email" name="form_ticket_meta_user_email" type="text" readonly="readonly" value="<?php echo $form_ticket_current_user_email; ?>">
<input name="submit" type="submit" value="Submit Ticket" />
</form>
In the script.js
I have:
<script>
$(function(){
$("#mb_ticket_submit").submit(function(a){
a.preventDefault();
$.ajax({
method: "POST",
url: "validation.php",
data: {
mb_ticket_subject: $("#mb_ticket_subject").val(),
mb_ticket_content: $("#mb_ticket_content").val(),
mb_ticket_tax_stage: $("#mb_ticket_tax_stage option:selected").val(),
mb_ticket_tax_application: $("#mb_ticket_tax_application option:selected").val(),
mb_ticket_meta_date: $("#mb_ticket_meta_date").val(),
mb_ticket_meta_user_name: $("#mb_ticket_meta_user_name").val(),
mb_ticket_meta_user_email: $("#mb_ticket_meta_user_email").val(),
},
success: function(a){ $("div#response").show().html(a); },
});
})
});
</script>
Then finally the validation.php
has the following (the $var are cleaned up, and validated from their $_POST[]
:
if( empty($form_ticket_meta_user_name) ) { die( 'Doesn\'t look like you have logged in properly'; ) }
if( empty($form_ticket_meta_user_email) ) { die( 'Doesn\'t look like you have logged in properly'; ) }
if( !empty($form_ticket_meta_user_email) ){ if( !filter_var($form_ticket_meta_user_email, FILTER_VALIDATE_EMAIL) ) { die( 'Doesn\'t look like your email address is formatted properly. Contact your administrator.' ); } }
if( empty($form_ticket_subject) ) { die( 'No subject' ); }
if( empty($form_ticket_content) ) { die( 'No content' ); }
if( empty($form_ticket_tax_stage) ) { die( 'No stage'); }
if( empty($form_ticket_tax_application) ) { die( 'No app' ); }
if( empty($form_ticket_tax_priority) ) { die( 'No priority' ); }
if( empty($form_ticket_tax_location) ) { die( 'No location' ); }
Thanks :)