Rzj Hayabusa
Rzj Hayabusa

Reputation: 657

How to pass value into .post Serialize using jQuery?

I have these jQuery code.

I generate 6 digits random number and I need to pass it into .post Serialize every time button is clicked. I want the number generate inside jQuery click function.

Somehow I cannot pass the value of "var tac_no" into .post Serialize.

Here's my code for jQuery

$('#request_code').click(function(e){ 
            var tac_no = Math.floor(100000 + Math.random() * 900000);
            var data_tac = $("#form-tac").serialize()+"&type="+"updateTable";

            $.post("php/ajax-request-tac.php",data_tac).done(function( data ){
            if(data==true)
            { 
                alert('Request Success');  
            }
            else if(data==false)
            {
                alert('Request failed');
            }  
            });   
            return false;
        });

And here's my php code

<?php
            require 'conn.php';
            function oracle_escape_string($str) 
            {
              return str_replace("'", "''", $str);
            }

            if((isset($_POST['type'])) && ($_POST['type']=='updateTable'))
            { 
                $sqlNextSeq         = "SELECT SEQ_TAC.nextval AS RUNNO FROM DUAL";
                $stid2              = oci_parse($ociconn,$sqlNextSeq);
                oci_execute($stid2);
                $row                = oci_fetch_array($stid2, OCI_RETURN_NULLS); 

                $query = "insert into table
                                                    (
                                                        ID, 
                                                        TAC_NO
                                                    ) 

                                            values 
                                                    (
                                                        '".$running_no."', 
                                                        '".oracle_escape_string($_POST['tac_no'])."', 
                                                    )
                    ";
                oci_execute(oci_parse($ociconn, $query));       
                echo true;
            }
        ?>

Any idea how to do that ?

Appreciate if someone can help me. Thanks.

Upvotes: 0

Views: 37

Answers (2)

Rahul
Rahul

Reputation: 18557

Append that number to tac_no directly,

$('#request_code').click(function(e) {
    var tac_no = Math.floor(100000 + Math.random() * 900000);
    var data_tac = $("#form-tac").serialize() + "&type=" + "updateTable"+"&tac_no="+tac_no;
    $.post("php/ajax-request-tac.php", data_tac).done(function(data) {
        if (data == true) {
            alert('Request Success');
        } else if (data == false) {
            alert('Request failed');
        }
    });
    return false;
});

Anyway its front end code, user can always see what you are sending, so security won't be covered.

Upvotes: 0

Daniel G
Daniel G

Reputation: 549

Personally, I'd generate the random number in PHP, but I think the issue is that you're not passing the data properly into PHP with thepost() function:

$.post("php/ajax-request-tac.php",data_tac).done(function( data ){

Instead you should pass it in as an object:

$.post("php/ajax-request-tac.php",{data_tac: data_tac, tac_no: tac_no}).done(function( data ){

Upvotes: 1

Related Questions