我查阅的所有教程只显示了如何使用表单进行工作。我想避免使用表单。现在我有Javascript的代码映射我的回答基于点击什么,并使警报中显示的数组。 (最后,我不希望它处于警戒状态,但是直到我知道所有工作都会保持在那里),但是我认为我有ajax将信息发送到服务器,但我不确定它是否工作。或者如何让php获取ajax信息并将其发送到服务器。我的服务器也连接正常。我发布了所有当前的代码。任何帮助表示赞赏。即使你只是给我一个链接或指向我的方向,这将是伟大的。我需要帮助使用Ajax,PHP和mySQL将信息发送到我的服务器而不使用表格
我认为我最大的问题是如何把JavaScript中生成的数组,然后调用Ajax,然后Php。因为我已经创建了数组,我在Ajax中调用了什么。那么如何用PDO
<?php
include_once('database.php');
$name = $_POST['name'];
$choices = $_POST['answers'];
if(mysql_query("INSERT INTO answers VALUES('$name', '$choices')"))
echo "Successfully Inserted";
else
echo "Insertion Failed";?>
jQuery的
$(document).ready(function() {
$(function() {
//catch this values, because you will use these for more than one time
var answers = [];
function getAnswers() {
answers = []; //empty old answers so you can update it
$.map($('.on'), function(item) {
answers.push($(item).data('value'));
});
}
//init the answers in case you use it before click
getAnswers();
$(document).on('click', 'img', function(e) {
e.preventDefault();
$(this).toggleClass('on');
//trigger the state change when you click on an image
$(document).trigger('state-change');
});
//get answers when event was triggered
$(document).on('state-change', function(e) {
getAnswers();
});
$('#btn-show').click(function() {
alert(answers.join(',') || 'nothing was selected');
});
});
});
CSS
#seasoning {
margin: 8px;
font-size: 16px;
height: 100px;
width: 100px;
}
/* this shows the user which item has been selecting along with making a searchable class for the program */
.on {
padding: 10px;
background-color: red;
}
HTML
<html>
<head>
<LINK REL=StyleSheet HREF="code.css" TYPE="text/css">
<script type="text/javascript" src="javascript.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<form>
Name:
<input type="text" name="name">
</form>
<img src="https://i.imgur.com/7HyU4yh.jpg" id="seasoning" data-value="0">
<br>
<img src="https://i.imgur.com/OEHCjCK.jpg" id="seasoning" data-value="1">
<br>
<button id="btn-show">Submit</button>
</body>
</html>
治疗呢
我不没有看到您的JavaScript中的任何Ajax请求。 – jeroen 2014-10-17 20:09:44
我想可能是因为意外而误将我的php代码改写为ajax,并且忘记了它。现在不适合工作 – tearisha 2014-10-17 20:12:07
请确保您将有关从“click”功能中点击哪个元素的信息传递给其他功能。 – jeroen 2014-10-17 20:14:02