我想通过 AJAX POST 请求发送用户输入的字符串并添加到数据库:
Új gyártó: <input type="text" id="new_mfg" size="20">
<button id="add_mfg">Hozzáadás</button>
<script>
$("#add_mfg").click(function(e){
e.preventDefault()
var new_mfg = $('#new_mfg').val();
//$(this).text(new_mfg);
request = new XMLHttpRequest();
request.open("POST","/phps/add_mfg.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
request.send('$new_mfg');
});
</script>
这是 add_mfg.php 脚本:
require_once('login.php');
$new_mfg = $_POST['new_mfg'];
$new_mfg = stripslashes($new_mfg);
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if(!$db_server) die("Unable to connect to MYSQL:" . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database:" . mysql_error());
$query = "INSERT INTO mfg(ID, mfg_name) VALUES(NULL, '$new_mfg')";
$result = mysql_query($query);
mysql_close($db_server);
代码不起作用,我不知道问题出在哪里。