Reputation: 1330
i wanna enter a value passed by javascript function to the database. is there a way to do that? thanks.
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>
i wanna insert the value of engaged, booked etcc to the database. is there a way to do that? thanks.
Upvotes: 0
Views: 1876
Reputation: 7569
You don't seem to be using a <form>
by all those buttons you have there so let's just use a simple AJAX for your task. You already have a function all you need is another process to throw your stuff to the server and save it.
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);
}
In insert.php
you would get your POST data:
$Value = $_POST['value'];
// do database connections and inserts here
$Response = array('Success' => true, 'Message' => 'Inserted!');
echo json_encode($Response);
I hope you get the idea. There's a bunch of tutorials out there regarding this so give yourself a hand.
Upvotes: 1