When I use ajax to run a php/mysql query and return the value, it returns the that the ajax is in.
AJAX/JQUERY
$(document).off("click", "#create_warning");
$(document).on("click", "#create_warning", function() {
var comp_status = $("#comp_status").val();
var comp_name = $("#comp_name").val();
var comp_acct_num = $("#comp_acct_num").val();
var comp_country = $("#comp_country").val();
var comp_address = $("#comp_address").val();
var comp_address2 = $("#comp_address2").val();
var comp_city = $("#comp_city").val();
var comp_state = $("#comp_state").val();
var comp_zip = $("#comp_zip").val();
var comp_contact = $("#comp_contact").val();
var comp_phone = $("#comp_phone").val();
var comp_fax = $("#comp_fax").val();
var comp_email = $("#comp_email").val();
var comp_comment = $("#comp_comment").val();
var comp_id = <?php echo $members_id; ?>;
var action = 'create_warning';
var dataString = 'comp_status=' + comp_status + '&comp_name=' + comp_name + '&comp_acct_num=' + comp_acct_num + '&comp_country=' + comp_country + '&comp_address=' + comp_address + '&comp_address2=' + comp_address2 + '&comp_city=' + comp_city + '&comp_state=' + comp_state + '&comp_zip=' + comp_zip + '&comp_contact=' + comp_contact + '&comp_phone=' + comp_phone + '&comp_fax=' + comp_fax + '&comp_email=' + comp_email + '&comp_comment=' + comp_comment + '&comp_id=' + comp_id + '&action=' + action;
$.ajax({
type: "POST",
url: "custom/warning_list/warn-action.php",
//cache: true,
data: dataString,
//dataType: "text",
success: function(response){
alert(response);
//activateID2 = response;
//alert(activateID2);
$("#agreementFormy").show();
$("#addNewForm").hide();
$("#warningDialog").scrollTop(0);
if (pathname == '/my-warning-list.html') {$("#warn-data" ).load("my-warning-list.html #warn-data");}
},
});
return false;
});
THE PHP
$twelve_digit = rand(100000, 999999);
$twelve_digit .= rand(100000, 999999);
//ob_start();
$reporter_name = addslashes($_POST['reporter_name']);
$comp_status = $_POST['comp_status'];
$comp_name = $_POST['comp_name'];
$comp_acct_num = $_POST['comp_acct_num'];
$comp_country = $_POST['comp_country'];
$comp_address = addslashes($_POST['comp_address']);
$comp_address2 = addslashes($_POST['comp_address2']);
$comp_city = $_POST['comp_city'];
$comp_state = $_POST['comp_state'];
$comp_zip = $_POST['comp_zip'];
$comp_contact = $_POST['comp_contact'];
$comp_phone = $_POST['comp_phone'];
$comp_fax = $_POST['comp_fax'];
$comp_email = $_POST['comp_email'];
$comp_comment = addslashes($_POST['comp_comment']);
$warn_mem_id = $_POST['comp_id'];
$stmt = $pdo->prepare("INSERT INTO warning_list (reporter_name, warn_mem_id, comp_status, comp_name, comp_acct_num, comp_country, comp_address, comp_address2, comp_city, comp_state, comp_zip, comp_contact, comp_phone, comp_fax, comp_email, comp_comment, rebutt_numb) VALUES (:reporter_name, :warn_mem_id, :comp_status, :comp_name, :comp_acct_num, :comp_country, :comp_address, :comp_address2, :comp_city, :comp_state, :comp_zip, :comp_contact, :comp_phone, :comp_fax, :comp_email, :comp_comment, :rebutt_numb)");
$stmt->execute(array(
'reporter_name' => $reporter_name,
'warn_mem_id' => $warn_mem_id,
'comp_status' => $comp_status,
'comp_name' => $comp_name,
'comp_acct_num' => $comp_acct_num,
'comp_country' => $comp_country,
'comp_address' => $comp_address,
'comp_address2' => $comp_address2,
'comp_city' => $comp_city,
'comp_state' => $comp_state,
'comp_zip' => $comp_zip,
'comp_contact' => $comp_contact,
'comp_phone' => $comp_phone,
'comp_fax' => $comp_fax,
'comp_email' => $comp_email,
'comp_comment' => $comp_comment,
'rebutt_numb' => $twelve_digit
));
//get last inserted id and return to ajax
$lastid = $pdo->lastInsertId();
//$lastid = json_encode($lastid);
echo $lastid;
exit;
I am inserting the data string (this works) then getting the last inserted id to return to the ajax success response. But it returns the script code before the last inserted id.
Any help would be appreciated… Thanks!