你好我正在使用这个html表单:
<form action="" id="foo">
<select name="selectOption" class="dropdown-select">
<option value="">Select a day!</option>
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
</select>
<input type='radio' name='group2' id='shampstyle' value='Shampoo-Style'>
<label for='shampstyle'>Shampoo And Style-$50</label>
<input type='radio' name='group2' id='relaxer' value='Relaxer'>
<label for='relaxer'>Relaxer-$75</label>
<input type="submit" value="submit" class="btn btn-primary active">
</form>
这是ajax代码:
<script>
/* Attach a submit handler to the form */
$("#foo").submit(function(event) {
/* Stop form from submitting normally */
event.preventDefault();
/* Clear result div*/
$("#result").html('');
/* Get some values from elements on the page: */
var values = $(this).serialize();
/* Send the data using post and put the results in a div */
$.ajax({
url: "server.php",
type: "post",
data: values,
success: function(data){
if (data === "1") {
alert('appointment recieved');
};
},
error:function(){
alert("failure");
$("#result").html('There is error while submit');
}
});
});
//$("#showFrSent").fadeIn();
</script>
和server.php很简单:
include 'db.php';
$user_id = 1;
$selectOption = $_POST['selectOption'];
$radioVal = $_POST['group2'];
$newSql = "INSERT INTO appointment(user_id, weekday, service) VALUES ('$user_id', '$selectOption', '$radioVal')";
echo "1";
但问题是成功方法运行和警报'appointment recieved'
,但我看到没有数据插入数据库表。这可能会出错。我在浏览器上运行带有硬编码值的server.php,它可以插入到数据库中。但当我作为ajax运行时,它无法插入。
答案 0 :(得分:0)
您没有执行SQL(并且SQL注入是另一种锡或蠕虫)。
您使用的是PDO,MySqli(或...... ahh MySql)库
答案 1 :(得分:0)
您没有执行查询?
例如,如果您使用mysql使用mysql_query()
mysql_query("INSERT INTO appointment(user_id, weekday, service) VALUES
('$user_id', '$selectOption', '$radioVal')");