ajax提交到mysql_从PHP插入到MySQL(jQuery / AJAX)

嗨,这里只是一个简单的例子:

HTML:

Quick JQuery Ajax Request

var ajaxSubmit = function(formEl) {

// fetch where we want to submit the form to

var url = $(formEl).attr('action');

// fetch the data for the form

var data = $(formEl).serializeArray();

// setup the ajax request

$.ajax({

url: url,

data: data,

dataType: 'json',

success: function() {

if(rsp.success) {

alert('form has been posted successfully');

}

}

});

// return false so the form does not actually

// submit to the page

return false;

}

onSubmit="return ajaxSubmit(this);">

Value:

process.php脚本:

function post($key) {

if (isset($_POST[$key]))

return $_POST[$key];

return false;

}

// setup the database connect

$cxn = mysql_connect('localhost', 'username_goes_here', 'password_goes_here');

if (!$cxn)

exit;

mysql_select_db('your_database_name', $cxn);

// check if we can get hold of the form field

if (!post('my_value'))

exit;

// let make sure we escape the data

$val = mysql_real_escape_string(post('my_value'), $cxn);

// lets setup our insert query

$sql = sprintf("INSERT INTO %s (column_name_goes_here) VALUES '%s';",

'table_name_goes_here',

$val

);

// lets run our query

$result = mysql_query($sql, $cxn);

// setup our response "object"

$resp = new stdClass();

$resp->success = false;

if($result) {

$resp->success = true;

}

print json_encode($resp);

?>

请注意,这些都没有被测试.我希望它能帮助你.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值