console errors?
Jan 21, 2023
39
Level 1
How to use Javascript to trigger php if(isset)
Hello, good day all. Please i need some help. I am trying to use javascript and sweetalert to send request to external if(isset()) php statement, but i don't know if i am doing it correctly, the form is not submitting. Here is my code
// Javascript function
function submitForm(form) {
Swal.fire({
title: 'Are you sure?',
text: 'You want to perform this action!',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, Send!'
}).then((result) => {
if (result.isConfirmed) {
form.submit();
}
})
return false
}
My form button that is being triggered (Please note that a form already sent them here, this is just a preview page to confirm their action)
<form method="post" action="" onsubmit="return submitForm(this)">
<div class="localpreview">
<p>Receiver: <span><?php echo $_SESSION['receiver_last']." ".$_SESSION['receiver_first']?></span></p>
<p>Account No: <span><?php echo $_SESSION['account_no']?></span></p>
<p>Amount (Inc. Charges): <span><?php echo $symbol.number_format($_SESSION['debit'])?></span></p>
<p>Naration: <span><?php echo $_SESSION['naration']?></span></p>
<span class="transfer-btn">
<button type="submit" name="con_trans" id="con_trans">Send</button>
</span>
</div>
</form>
And my php code i am sending to
if(isset($_POST['con_trans'])) {
$timestamp = date('Y-m-d H:i:s');
$token = random_str(7, 'abcdefghijklmnopqrstuvwxyz');
$receiver_last = $_SESSION['receiver_last'];
$receiver_first = $_SESSION['receiver_first'];
$account_no = $_SESSION['account_no'];
$amounty = $_SESSION['debit'];
$naration = $_SESSION['naration'];
prepared_insert($pdo, 'transfers', [
'ref_id' => $token,
'amount' => $amount,
'naration' => $naration,
'status' => $status,
'created_at' => $timestamp,
'updated_at' => $timestamp
]);
}
Please or to participate in this conversation.