3

i have an application in javascript. I follow some tutorial to do it, but i really don't have experience with the javascript code. The problem is that i need to pass the variables results from javascript to mysql database. I have found some answers in this site and i try to do what i found with no luck. What i found is that i need ajax and php. I never use ajax and because of that i dont understand what i'm doing wrong.

Maybe if i put the code here, someone can help me with a solution.

This is the javascript code:

   

    function ajaxFunction(){
    var ajaxRequest;  

    try{
        Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }

    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            document.myForm.time.value = ajaxRequest.responseText;
        }
    }

      ds = new Date();
      e_time = ds.getTime();


      var res = new Object();//This are the results variables that i need to pass to my database
      res.bytes_transfered =;         
      res.total_time_seconds = (e_time-s_time)/1000;
      res.generatied_in = ;
      res.ip = "";
     -->
     var res1= 'res.bytes_transfered';
     var res2= 'res.total_time_seconds';
     var res3= 'res.generatied_in';
     var res4= 'res.ip';

     $.post('insert.php',{res.bytes_transfered:res1,res.total_time_seconds: res2, res.generatied_in: res3, res.ip:res4});

    var queryString = "?res.bytes_transfered=" + res.bytes_transfered + "&res.total_time_seconds=" + res.total_time_seconds + "&res.generatied_in =" + res.generatied_in + "&res.ip  =" + res.ip;
    ajaxRequest.open("POST", "insert.php" + queryString, true);
    ajaxRequest.send(null); 
     new Ajax.Request('insert.php', {

    onSuccess : function(xmlHTTP) {

        eval(mlHTTP.responseText);
    }

});
     

This is the insert.php:

        $fecha= date("Y-m-d H:i:s");
        $connnect= mysql_connect("localhost", "root", "xxxxxxxxx");
        mysql_select_db("dbname");

        $res1= mysql_real_escape_string($_POST['res1']);
    $res2= mysql_real_escape_string($_POST['res2']);
    $res3= mysql_real_escape_string($_POST['res3']);
    $res4= mysql_real_escape_string($_POST['res4']);

                    $queryreg=mysql_query("INSERT INTO grafico(Cantidad, Tiempo, IP, Bajada, Subida, Fecha) VALUES ('$res1','$res2','$res3','$res4','0','$fecha') ");
                if (!$queryreg) {
                die('No se ha podido ingresar su registro.');
                    }
                    else{

                        die("Usted se ha registrado exitosamente!");
                    }


I hope that somebody can help me. I dont know what to do!

2
  • You don't actually have that uncommented text in your code, do you? Commented Nov 12, 2012 at 2:57
  • Are you using jQuery? You don't have the tag listed but you are using jQuery code in your example. Commented Nov 12, 2012 at 10:02

3 Answers 3

0

It looks like your POST data has the keys and values backwards. In the data passed to $.post the key name needs to come first and the value after the :. So I think it should be:

$.post('insert.php',{res1:res.bytes_transfered,res2:res.total_time_seconds,res3:res.generatied_in, res4: res.ip});
Sign up to request clarification or add additional context in comments.

3 Comments

The user has given us nothing to suggest he is using jQuery, so why would you give him a snippet of jQuery code without further explanation?
Perhaps it was the line where he used $.post that I just copied and pasted and then corrected.
My apologies, I failed to notice the code block was scrollable. Hence why my answer was not terribly useful either.
0

What you need to do is have JavaScript pass your variables to PHP, which in turn will use it in your MySQL statements (most probably via PDO in PHP).

Now, what is AJAX then? Well it is the modern way that will help you send data from JavaScript to PHP and get a response back from PHP to JavaScript WITHOUT the need to refresh or reload the page.

So in conclusion, JavaScript makes an AJAX call, that call will send data to PHP which will do something with MySQL, and then respond back to JavaScript with your results.

Comments

0

You need to comment out a couple of lines so that it won't be interpreted as code

//Opera 8.0+, Firefox, Safari

Your Ajax code only creates the ajax object and sets up an event listener, it never actually makes a request, so of course it cannot work. The request would look something like this

ajaxRequest.open("post", "/myphppage.php", true);
...
ajaxRequest.send("somevariable=" + variable);

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.