0

I am trying to create a table in MySQL dynamically from PHP.

User inputs should be inserted into that table using jQuery,AJAX and PHP.

my code is looking like this.

var toServer = brandname+','+prodname+','+qty;
$.ajax({
type: 'POST',
url: 'sample.php',
data: toServer,
cache: false,
success: function(res){
alert(res);
},
error: function(){
alert(1);
}
})

In sample.php:

require '../models/orderClass.php';
$order = new orderClass;
$data = explode(',',$_REQUEST['toServer']);
$res = $order->addToCart('[email protected]', $data[0], $data[1],$data[2]);
echo $res;

In orderClass:

$this->con = new mysqli('servername','user','pass','dbnaem');
if($this->con->connect_error)
{
echo($this->con->connect_error);
}

Without AJAX, its working. After adding $.ajax() its not working. Please help me out Thanks in advance

I am new to AJAX and I added error: function at last since it was not working.

1
  • I think your data parameter is wrong should be used like data: { name: "John", location: "Boston" } Commented Jun 7, 2014 at 13:16

2 Answers 2

1

because the "data: " sent to server in $_REQUEST so it must be a hash of data.
try this:

var toServer = brandname+','+prodname+','+qty;
$.ajax({
    type: 'POST',
    url: 'sample.php',
    data: {'toServer': toServer},
    cache: false,
    success: function(res){
        alert(res);
    },
    error: function(){
        alert(1);
    }
})

or another version:
javascript:

$.ajax({
    type: 'POST',
    url: 'sample.php',
    data: {
        'brandname': brandname,
        'prodname': prodname,
        'qty': qty
    },
    cache: false,
    success: function(res){
        alert(res);
    },
    error: function(){
        alert(1);
    }
});

php part:

require '../models/orderClass.php';
$order = new orderClass;
$brandname = $_REQUEST['brandname'];
$prodname = $_REQUEST['prodname'];
$qty = (int)$_REQUEST['qty'];
$res = $order->addToCart('[email protected]', $brandname, $prodname, $qty);
echo $res;
Sign up to request clarification or add additional context in comments.

1 Comment

nice! happy to help (:
0

your variable should b like this

var toServer = "brandname="+brandname+" &prodname= "+prodname+" &qty="+qty;

and in sample.php

use like this

$brandname =  $_REQUEST['brandname'];

$prodname=  $_REQUEST['prodname'];

$qty=  $_REQUEST['qty'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.