Lodeweyk Andri
Lodeweyk Andri

Reputation: 43

cannot get value from $_post

I have an question about my code.

I use $ post to send the array to do.insert_php.

$(document).ready(function(){  
    $('#submit_button').click(function(){
        if (confirm("Are you sure you want to Process this form?")){
            var form=$('#form2').serialize();
            $.ajax({
                url:"do/do_insert.php",
                method:"POST",
                data:form,
                success:function(){
                    $('#form2')[0].reset();
                    alert('Process Success');
                } 
            });
        }
    });
});

and then looping the value in the do_insert process.

foreach($_GET["contract"] as $k=>$val1){
//code here
}

this is my dynamic field for contract.

$data1 = json_decode($_POST['myData']);
//$data = json_decode(stripslashes($_POST['data']));
if (is_array($data1) || is_object($data1))
{
    $count = 0;
    echo "<tr id='row'> ";
    foreach($data1 as $mydata){
        $count++;

        //echo "<td width='197'><input type='text' id='contract.".$mydata->Id."' name= 'contract[]' value='".$mydata->Id."'/></td>";
        echo "<td width='197'><input type='hidden' id='contract.".$mydata."' name= 'contract[]' value='".$mydata."'/></td>";


   }
   echo "</tr> ";
   echo "<tr id='row'> ";
   echo "<td width='197'>Total Record</td>";
   echo "<td width='197'><input type='text' id='total' name= 'total' value='".$count."'/></td>";
   echo "</tr> ";
}

my form

<form id="form2" name="form2" method="POST">
<td><input name='submit_button' type='button' id='submit_button' value='Submit' class='btn'/></td>
<table id="dynamic_field">

</table>


</form>

the question is, when I use $_get in the do_insert.php,the process will be running, but when I change it into $_post, it will be error, and the error is "Undefined index: contract"

Can you help me with this? Thanks before

Upvotes: 1

Views: 77

Answers (1)

Saroj Shrestha
Saroj Shrestha

Reputation: 2875

Try with type:"POST", instead of method:"POST",.

method: is an alias for type: but only since jQuery 1.9

Upvotes: 1

Related Questions