CS.Han
CS.Han

Reputation: 13

I have some trouble in jquery ajax post and php

I have a problem with jQuery AJAX post and PHP.

I try to send data using .serialize() and add some additional data.

But in PHP, I received .serialize() data but not additional data.

In my code jquery ajax,

         function load() {
            var form_data = $('#frm2').serialize();
            form_data.push({name:'year',value:'2016'});
            alert(form_data);
            $.ajax({
                type : "POST",
                url : "insert.php",
                cache : false,
                async : false,
                data : form_data,

                success : function(html) {
                    alert(form_data);
                }
            });
          }

and frm2 (form's id) is:

          <form name='frm2' action="./BOMinsert.php" id = 'frm2' method='post'>
            <table width="30%" id="keytable" style="border: 1px gray solid; table-layout: fixed">
                <thead style="background: #EAEAEA">
                    <th>year</th>
                    <th>brand</th>
                    <th>season</th>
                    <th>Styleno</th>
                </thead>
                <tbody style="text-align: center">
                    <?php 
                    for($i =1;$i<5;$i++)
                        echo "<td>".$_POST['name'.$i]."</td>";
                    ?>
                </tbody>
            </table>
            <BR>
            <BR>
            <table id="bomtable" style="width: 100%;table-layout: fixed">
                <thead style="background: #EAEAEA">
                    <th>item1</th>
                    <th>item2</th>
                    <th>item3</th>
                    <th>item4</th>                          
                    <th>item5</th>
                    <th>item6</th>
                    <th>item7</th>
                    <th>item8</th>
                    <th>item9</th>
                    <th>item10</th>
                    <th>item11</th>
                    <th>item12</th>
                </thead>
                <tbody style="text-align: center">
                    <?php echo "<td>".$_POST['name5']."</td>" ?>
                    <?php echo "<td>".$_POST['name6']."</td>"?>
                    <?php echo "<td>".$_POST['name7']."</td>"?>
                    <td><input type="text" class="ui-widget ui-corner-all" name='item' size="5" maxlength="10"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='description' size="8" maxlength="30"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='supplycompany' size="5" maxlength="10"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='colorway' size="5" maxlength="10"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='unit' size="5" maxlength="10"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='size' size="5" maxlength="10"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='consume' size="5" maxlength="10"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='unit' size="5" maxlength="10"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='etc'/></td>
                </tbody>
                <input type='submit' value='save' class="ui-button ui-widget ui-corner-all" style="background: #3498DB; color: white;float: right" onclick="load();">
          </form>

and finally my PHP code:

  if(isset($_POST['year'])){
    echo $_POST['year'];
  }else{
   var_dump($_POST);
  }

PHP results:

array(8) { ["item"]=> string(0) "" ["description"]=> string(0) "" ["supplycompany"]=> string(0) "" ["colorway"]=> string(0) "" ["unit"]=> string(0) "" ["size"]=> string(0) "" ["consume"]=> string(0) "" ["etc"]=> string(0) "" } 
Notice: Undefined index: year in C:\Apache24\htdocs\ptest\BOMinsert.php on line 19

p.s. I also tried .serializeArray(), .push(), .serialize()+"&year=2016".

Code above tested but does not work.

Upvotes: 1

Views: 45

Answers (2)

moni_dragu
moni_dragu

Reputation: 1163

Why not add a hidden field to your form, with the name and value you need to pass?

<input type="hidden" name="year" value="2016" />

UPDATED to use dynamic year value:

<input type="hidden" name="year" value="<?php echo $_POST['name1']; ?>" />

Upvotes: 0

Dennisrec
Dennisrec

Reputation: 333

First the serverside php code is below..

<?php

if(isset($_POST['year'])){

    #Now the POST['year'] Request is available to be checked;
    #Go ahead and try it..

    $data=$_POST['year'];
    echo  $data;
    exit;
}
?>

And the the simple treak it using the url

<script>
    function load() {
        var form_data = $('#frm2').serialize();
        var year='2016';
        //As you can see this will work perfectly and is already tested!
        //Just append the required values to the url ((in Url form like below)) and it will send everything to the same page


        $.ajax({
            type : "POST",
            url : "demo.php?year="+year,
            cache : false,
            async : true,//not false for the best user experience...
            data : form_data,

            success : function(html) {
                console.log(html)
            }
        });
    }

    //remember to bind an event on an element to call this function to work.
    //I hope it helps...

</script>

Upvotes: 1

Related Questions