I have a form that modifies meta content of table 'wp_postmeta'. I'm trying to figure out a way to add the form results to another table (transactions).
I have the php setup and it works if I remove the "id" value from the submit input section. With ID included, the form modifies wp_postmeta but no data is sent to "transactions" table. Without ID included, the opposite. Any suggestions?
FORM Submit button:
<div class='inventory-management-form-item-holders inventory-management-submit-holder'>
<input type="submit" name="submit" value="SUBMIT" id="im-submit">
</div>
"im-submit" js SECTION:
// submit form
jQuery("#im-submit").click(function(){
// Change thw loading Gif url
jQuery('#the-results-holder').html("<img src='/wp-content/plugins/mcs-inventory-management/assets/images/loading_icon.gif' />");
jQuery.ajax({
type: "POST",
data: {'im-product-id': theProductId, 'status-quantity': jQuery('#status-quantity').val(), 'status-action': jQuery('#im-action-box').val(), 'status-location': jQuery('#status-location').val(), 'status-move-location': jQuery('#status-move-location').val()},
dataType: "json",
url: "/inventory-management-process/",
processData: true,
success: function(data) {
jQuery('#the-results-holder').html("");
jQuery('#status-stock').val(data.stock);
jQuery('#status-res').val(data.res);
jQuery('#status-prepro').val(data.prepro);
jQuery('#status-clean').val(data.clean);
jQuery('#status-quantity').val("0");
},
error: function (xhr, ajaxOptions, thrownError) {
jQuery('#the-results-holder').html("");
jQuery('#the-results-holder-error').html("There has been an error!");
}
});
return false;
});
PHP That uses INSERT Query for "transaction" table
<form name='inventory-management' id='inventory-management-form' method="POST" action='<?php $transaction ?>'>
<?php
$product= $_POST['part-number'];
$action= $_POST['status-action'];
$from_location= $_POST['status-location'];
$to_location= $_POST['status-move-location'];
$qty= $_POST['status-quantity'];
if(isset($_POST['submit']))
{
$transaction = "INSERT INTO transactions (id,product,action,from_location,to_location,qty) VALUES ('','$product','$action','$from_location','$to_location','$qty')";
$result = mysql_query($transaction);
}
?>
it works if you remove the "id" value from the submit input section because the form is submitted by the browser and not by your js code and then the submit
value is in the $_POST
.
to make your jQuery code works you need to add the submit
value to the post, something like this:
// submit form
jQuery("#im-submit").click(function() {
// Change thw loading Gif url
jQuery('#the-results-holder').html("<img src='/wp-content/plugins/mcs-inventory-management/assets/images/loading_icon.gif' />");
var ajax_config = {
type: "POST",
data: {
'submit': 'submit',
'im-product-id': theProductId,
'status-quantity': jQuery('#status-quantity').val(),
'status-action': jQuery('#im-action-box').val(),
'status-location': jQuery('#status-location').val(),
'status-move-location': jQuery('#status-move-location').val()
},
dataType: "json",
url: "/inventory-management-process/",
processData: true,
success: function(data) {
jQuery('#the-results-holder').html("");
jQuery('#status-stock').val(data.stock);
jQuery('#status-res').val(data.res);
jQuery('#status-prepro').val(data.prepro);
jQuery('#status-clean').val(data.clean);
jQuery('#status-quantity').val("0");
},
error: function(xhr, ajaxOptions, thrownError) {
jQuery('#the-results-holder').html("");
jQuery('#the-results-holder-error').html("There has been an error!");
}
};
jQuery.ajax(ajax_config);//jajax # 1
//change the url
ajax_config.url = jQuery('#inventory-management-form').attr("action");
jQuery.ajax(ajax_config);//jajax # 2
return false;
});
EDIT:
i think you are posting to two different locations, that's why without the ID, it adds the form results to transactions table and with the ID, it updates the wp_postmeta table. you should try using the ID, and in the jQuery code, make 2 ajax call, one just like you have and another changing the url to the form action value.
'url': jQuery('#inventory-management-form').attr("action")