0

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);

コードが機能せず、何が問題なのかわかりません。

4

2 に答える 2

3

クリックイベントハンドラーをラップする必要があります$(document).ready(function(){ });

$(document).ready(function(){  
$("#add_mfg").click(function(e){

});
});
于 2013-08-26T11:13:27.093 に答える
0

この短いバージョンを使用してください:

$(function() {
  $("#add_mfg").click(function(e){
      // ...
  });
});
于 2013-08-26T11:18:15.757 に答える