我想输入javascript函数传递给数据库的值。有没有办法做到这一点?感谢。
var eng = 0;
var nothome = 0;
var nointerest = 0;
var callback = 0;
var booked = 0;
function myFunction(i,txt, elemid){
var plural;
if (i != 1) plural = "s.";
else plural = ".";
document.getElementById(elemid).innerHTML = txt + i + " time" + plural;
alert(txt + i + " time" + plural);
}
<button onclick="eng += 1; myFunction(eng,'Engaged: ','Engaged');">Engaged</button><span name="Engaged" id="Engaged"></span>
<button onclick="nothome += 1; myFunction(nothome,'Not Home: ','nothome');">Not Home</button><div id="nothome"></div>
<button onclick="nointerest += 1; myFunction(nointerest,'No Interest: ','nointerest');">No Interest</button><div id="nointerest"></div>
<button onclick="callback += 1; myFunction(callback,'Call Back: ','callback');">Call Back</button><div id="callback"></div>
<button onclick="booked += 1; myFunction(booked,'Booked: ','booked');">Booked</button><div id="booked"></div>
我想将参与,预订的etcc的值插入数据库。有没有办法做到这一点?感谢。
答案 0 :(得分:1)
你似乎没有使用那里的所有按钮<form>
,所以让我们只使用一个简单的AJAX来完成你的任务。你已经拥有了一个功能,你需要的是将你的东西扔到服务器并保存它的另一个过程。
function myFunction(i, txt, elemid)
{
var plural;
if (i != 1) plural = "s.";
else plural = ".";
document.getElementById(elemid).innerHTML = txt + i + " time" + plural;
//alert(txt + i + " time" + plural);
var params = "value=" + txt;
xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "insert.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close")
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = eval( "(" + xmlhttp.responseText + ")" );
alert(response.Message);
}
}
xmlhttp.send(params);
}
在insert.php
中,您将获得POST数据:
$Value = $_POST['value'];
// do database connections and inserts here
$Response = array('Success' => true, 'Message' => 'Inserted!');
echo json_encode($Response);
我希望你明白这个主意。这里有很多关于此的教程,请自己动手。