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 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 etc to the database. is there a way to do that? thanks.

what im trying to do here is when i click a button, the corresponding value increments by 1. and then i need to insert this value in the database. if not this way, is there any other way to do this??

you must use form tag in between, then define one hidden input. When user submits the form, set hidden input using your myfunction, and then process the form(insert the submited hidden value) in the form action page.

You can also use AJAX for that. Than the database is altered in the background, without the need to refresh the page.

Member Avatar for diafol

As twiss says, you can use Ajax for that. Only php (server-side) will insert to DB. SO you can either do it via normal form submit or via ajax.

Ajax pretty much just hijacks the submit procedure (or even a button click etc), does some jiggery pokery with js and php, so you don't need to refresh.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.