Ambi
Ambi

Reputation: 53

Invalid or unexpected token on php ajax

I'm trying to figure out why I'm having this error as shown below.

Uncaught SyntaxError: Invalid or unexpected token

it's pointing at the image below enter image description here

below is my ajax code

<script>
     
    $(document).ready(function() {
        $('#btn_stockin').click(function() {
    
            event.preventDefault();
            /*Reading current date*/
            var savedate = date('Y-m-d',strtotime(time()));
            
            /*Reading value from modal*/
            var newStock = $('#txt_addstock').val();
            var newPrice = $('#txt_addprice').val();
            if(newStock == '' && newPrice == ''){
                alert("Oops");
            }else{

                $.ajax({
                    method: 'POST', 
                    url: 'stock-in.php',
                    data:{stock_up: newStock, cost: newPrice, currentDate : savedate
                        <?php 
                            echo ', id: '.$row->pid.', oldstock: '.$row->pstock.', productcategory: '.$row->pcategory.', productname: '.$row->pname.''
                        ?>
                    },
                    success:function(data){
                        $('#add_stock_modal').modal('hide');
                        window.location.reload();
                    }

                });
            }

        });
    });


</script>

Please, what am I doing wrong, or have I done wrong on the above codes?

Upvotes: 1

Views: 206

Answers (1)

Zak
Zak

Reputation: 7515

You should be putting quotes around your JSON data ... It's fumbling on the "spaces" in the string because it's not in quotes.

<?php
echo ', id: "'.$row->pid.'", oldstock: "'.$row->pstock.'", productcategory: "'.$row->pcategory.'", productname: "'.$row->pname.'"'
?>

Upvotes: 1

Related Questions