Ajax, send different variables between files

Hi E's, yesterday here in EE, I get a good solution for send variables between two files using Ajax, jquery and php. But that solution, just send a single variable between files. I try change the code for send more then one variable between files, but without success, and that is the reason I open this new question.
I have the file index.php and the file receive.php, this one receive two variables and send a email for I test if work.
Index.php:
<html>
<head>
<title>ajax example</title>
<script src="jquery-ui.min.js" type="text/javascript"></script>
<script src="jquery.min.js" type="text/javascript"></script>
</head>
<body>
<script>
var send;
var other;
send = "00:11:40";
other = "test";

$(document).ready(function(){ 
$("#info").click(function(){ 
        $.ajax({ "url":"receive.php"
        		//line before change
                    //,"data": {"dataSent":send}
                    //line where I try send 2 variables
                     ,"data": {"dataSent":send, "dataother":other}
                    ,"success":function(data, textStatus, jqXHR){
                          alert( "the server sent the following response" + data );

                     }
                 }); 
    }); 

 }); 


</script>
<div id="info">444</div>
</body>
</html>

Open in new window

and this is the receive.php file:
<?
$info = $_GET["dataSent"];
$other = $_GET["dataother"];
$mensagem = "$info and $other";
$para = "xxxxxxx@xxxxxxx";
$assunto = "test ajax";
mail($para, $assunto, $mensagem);
?>

Open in new window

I try to edit this line: ,"data": {"dataSent":send} and change to this one:,"data": {"dataSent":send, "dataother":other}.
Not work.
My question is, what changes I have to do fore send more then one variable between index.php and receive.php?

The best regards, JC
LVL 3
Pedro ChagasWebmasterAsked:
Who is Participating?
 
Chris StanyonConnect With a Mentor Commented:
There's nothing wrong with your jQuery - it works fine.

You say it doesn't work but don't give us any details about exactly won't work. The only thing I can see is that your jQuery script is expecting some return value from your php script (data), but your script doesn't actually return (echo) anything out. Add this to the end of your receive.php file:

echo $mensagem;

Your script should then alert out the info, and you can see if it received the variables OK.

@jon and @ray : .post() will POST the info. The php script is expecting GET, so it should be .get() or .ajax(), - unless the receive.php script is changed.
0
 
hforhirenpatelConnect With a Mentor Commented:
Hi,joaochagas

 html>
<head>
<title>ajax example</title>
<script src="jquery-ui.min.js" type="text/javascript"></script>
<script src="jquery.min.js" type="text/javascript"></script>
</head>
<body>
<script>
var send;
var other;
send = "00:11:40";
other = "test";

$(document).ready(function(){
$("#info").click(function(){
        $.ajax({ url:"receive.php"
                    
                     ,data: {"dataSent":send, "dataother":other}
                    ,success:function(data, textStatus, jqXHR){
                          alert( "the server sent the following response" + data );

                     }
                 });
    });


 });


</script>
<div id="info">444</div>
</body>
</html>



remove double quotation ("")from url,data,success. shown in bold character and click on 444.

i checked whole this code mine is running.

if u have problem i will solve it.
0
 
Pedro ChagasWebmasterAuthor Commented:
Thanks for the possible solution. I'm out of office. I will check in few hours.
~JC
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
JonNormanCommented:
I wouldn't have thought that removing the double quotes would make any difference, you can simplify things by using the $.post function:
$(document).ready(function(){ 
    $("#info").click(function(){ 
        $.post("receive.php",{"dataSent":send, "dataother":other},function(data, textStatus, jqXHR){
            alert( "the server sent the following response" + data );
        });
    }); 
});

Open in new window

You can use firebug in firefox to see what was sent to recieve.php in the net panel.
0
 
Ray PaseurConnect With a Mentor Commented:
See http://www.laprbass.com/RAY_ajax_jquery.php

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<title>Ajax Example Using jQuery with Two Variables</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

<script>
$(document).ready(function(){
    $("#signal").click(function(){
        datax = $("#xinput").val();
        datay = $("#yinput").val();
        $.post("RAY_ajax_server.php", {x:datax, y: datay}, function(response){
            $("#output p#target").html(response);
        });
    });
});
</script>

</head>
<body>

<input id="xinput" value="Enter your name here" />
<input id="yinput" value="Enter your email here" />
<div   id="signal">This is not a link, but Click Me Anyway!</div>
<div   id="output">
   <p  id="static">This element remains unchanged</p>
   <p  id="target">This element gets the AJAX response</p>
</div>

</body>
</html>

Open in new window

The background script is here:
<?php // RAY_ajax_server.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');

// START THE OUTPUT BUFFER TO CAPTURE THE var_dump() DISPLAY
ob_start();

// MAKE THE OUTPUT EASY TO READ
echo '<pre>';

// SEND BACK THE DATE...
echo date('r');
echo  PHP_EOL;

// ... AND THE GET-REQUEST VARIABLES
echo 'GET: ';
var_dump($_GET);

// ... AND THE POST-REQUEST VARIABLES
echo 'POST: ';
var_dump($_POST);

echo '</pre>';
echo  PHP_EOL;

// CAPTURE THE OUTPUT BUFFER
$response = ob_get_clean();

// SEND THE CONTENTS OF THE OUTPUT BUFFER
die($response);

Open in new window

HTH, ~Ray
0
 
Pedro ChagasWebmasterAuthor Commented:
Hi @ChrisStanyon, you have reason, my original script work well. I believed in you, and I check where is the problem, and the problem was that one of .js files was corrupted, I change them and the script work.

My apologies to the other experts who strove to find a solution.

@Ray, great example, very useful.

The best regards, JC
0
 
Pedro ChagasWebmasterAuthor Commented:
@JonNorman, your solution not work.

~JC
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.