Mark Alan
Mark Alan

Reputation: 455

jquery form submit without hitting submit button

I have created a paypal form but I want to redirect the paypal information once all database queries are confirmed and inserted after that I would like then the form should be automatically submitted and redirected to paypal so I used some jQuery to do so but unfortunately when I reload the page he form did not submits can anyone help me out

Here is my code

<form action="listing.php" method="post">
                        <input type="hidden" name="business" value="<?php echo $paypal_id; ?>">
                        <input type="hidden" name="url" value="<?php echo $paypal_id; ?>">
                        <input type="hidden" name="paypal_url" value="<?php echo $paypal_url; ?>" />
                        <input type="hidden" name="cmd" value="_xclick">
                        <input type="hidden" name="item_name" value="Room Rent for <?php echo $data['name']; ?>">
                        <input type="hidden" name="item_number" value="<?php echo $_SESSION['id']; ?>">
                        <input type="hidden" name="credits" value="510">
                        <input type="hidden" name="userid" value="<?php echo $_SESSION['id']; ?>">
                        <input type="hidden" name="amount" value="<?php echo $total_cost; ?>">
                        <input type="hidden" name="start_time" value="<?php echo $start_time; ?>" />
                        <input type="hidden" name="end_time" value="<?php echo $end_time; ?>" />
                        <input type="hidden" name="agent_id" value="<?php echo $data['agent_id']; ?>" />
                        <input type="hidden" name="list_id" value="<?php echo $data['id']; ?>" />
                        <button type="submit" class="btn5 btn-primary btn-sm" name="book">Book Now</button>
                    </form> 

So let me explain what I did here is i have created 2 forms in first form I want user to click on submit when user submits all the information saves into the database after it save it generates with an auto id last id and I want that id as transaction id so I sent that id to paypal for and paypal without form i cannot send information so that is why I have used following steps to do so

  <?php
     if(isset($_POST['book'])) {
        $user_id   = $_POST['userid'];
        $agent_id  = $_POST['agent_id'];
        $list_id   = $_POST['list_id'];
        $business  = $_POST['business']; 
        $item_name = $_POST['item_name'];
        $item_num  = $_POST['item_number'];
        $amount    = $_POST['amount'];
        $item_num  = $_POST['item_number'];
        $start_time= $_POST['start_time'];
        $end_time  = $_POST['end_time'];
        $qry_sql   = mysqli_query($connection, "INSERT INTO bookings (user_id, agent_id, list_id, booking_date, start_time, end_time, price, status) VALUES ('$user_id', '$agent_id', '$list_id', '".date('d-m-Y')."', '$start_time', '$end_time', '$amount', 'Pending')");
        $trans_id  = mysqli_insert_id($connection); 
        if($qry_sql) {
            $co_ammount    = $amount / 100 * 25;
            $agent_ammount = $amount - $co_ammount;
            $due_date = date('d-m-Y', strtotime("+2 days"));
            $qry_sql1 = mysqli_query($connection, "INSERT INTO financial (agent_id, booking_id, order_date, due_date, total_ammount, agent_ammount, co_ammount, status) VALUES ('$agent_id', '$trans_id', '".date('d-m-Y')."', '$due_date', '$amount', '$agent_amount', '$co_ammount', 'Pending')");

?>
            <script>
            $('#frmPayPal1').submit();
            </script>
            <form action="<?php echo $_POST['paypal_url']; ?>" method="post" id="frmPayPal1">
                <input type="hidden" name="business" value="<?php echo $business; ?>">
                <input type="hidden" name="cmd" value="_xclick">
                <input type="hidden" name="item_name" value="<?php echo $item_name; ?>">
                <input type="hidden" name="item_number" value="<?php echo $trans_id; ?>">
                <input type="hidden" name="userid" value="<?php echo $user_id; ?>">
                <input type="hidden" name="amount" value="<?php echo $amount; ?>">
                <input type="hidden" name="no_shipping" value="1">
                <input type="hidden" name="currency_code" value="USD">
                <input type="hidden" name="handling" value="0">
                <input type="hidden" name="cancel_return" value="http://dev.uparksf.com/cancel.php?tx=<?php echo $trans_id; ?>">
                <input type="hidden" name="return" value="http://dev.uparksf.com/success.php">
            </form> 
    <?php } ?>      
<?php } ?> 

Upvotes: 0

Views: 147

Answers (2)

Darkman
Darkman

Reputation: 333

Place your submit statement after the form closing tag. Javascript is procedural so by placing the submit before the form you are submitting the form before it's loaded.

Upvotes: 0

Jameson the dog
Jameson the dog

Reputation: 1806

only one more thing you need to do, instead of

<script>
     $('#frmPayPal1').submit();
</script>

it should be:

 <script>
    $(document).ready(function(){
        $('#frmPayPal1').submit();
    })
 </script>

and that should work!

Upvotes: 1

Related Questions