0

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.

4
  • Short answer: AJAX. Long answer, javascript can't do it directly, but can talk to a PHP service which does the database work, and AJAX is the specific name for this technique. Commented Apr 28, 2011 at 2:18
  • i dont knw ajax.. is there a way to do it in PHP?? Commented Apr 28, 2011 at 2:23
  • @Marc B - Why would this require AJAX? A postback triggered by a JS form submit would work, as well. Commented Apr 28, 2011 at 2:32
  • @jared i tried using $_POST[''] but it doesnt seem to work.. i might be doing something wrong.. can you please help?? Commented Apr 28, 2011 at 2:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.