[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Ajax, send different variables between files

Posted on 2012-09-14
7
Medium Priority
?
764 Views
Last Modified: 2012-09-15
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
0
Comment
Question by:Pedro Chagas
7 Comments
 
LVL 2

Assisted Solution

by:hforhirenpatel
hforhirenpatel earned 800 total points
ID: 38398462
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
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 38398589
Thanks for the possible solution. I'm out of office. I will check in few hours.
~JC
0
 
LVL 13

Expert Comment

by:JonNorman
ID: 38398972
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 total points
ID: 38399402
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
 
LVL 45

Accepted Solution

by:
Chris Stanyon earned 400 total points
ID: 38400844
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
 
LVL 3

Author Closing Comment

by:Pedro Chagas
ID: 38401870
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
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 38401875
@JonNorman, your solution not work.

~JC
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question