gpt4 book ai didi

php - 将javascript函数的值插入数据库

转载 作者:行者123 更新时间:2023-11-29 14:50:36 24 4
gpt4 key购买 nike

我想输入一个由 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>

我想将已订婚、预订等的值插入数据库。有没有办法做到这一点?谢谢。

最佳答案

您似乎没有使用 <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);

我希望你能明白。有很多关于此的教程,所以请帮自己一把。

关于php - 将javascript函数的值插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5812822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com