1

I am trying to send a string via AJAX to a PHP file. I have two lottery wheels, each of them has a result (if you press on "spin"). I wish to receive the results and print them to an HTML file. Please see here: http://zeevm.co.il/rollet/

This is the AJAX code that is sending the first wheels result:

function sendwinnertophp(){
    var winner = $("#winner").html();
    $.ajax({
        type: "POST",
        url: "getwinner.php",
        data: {
            "winner": winner
        },
        cache: false,
        success: function(data){ 
        //  alert(data);
        },
        error: function(err){
            alert(err);
        }
    });
}

This is the PHP code that is saving it to the HTML file:

$file = fopen("log.html", "a");
$ip=$_SERVER['REMOTE_ADDR'];
$winner = $_POST['winner'];

//write the data
$time = date("H:i dS F");
fwrite($file, "<b>Time:</b> $time<br/><b>ip:</b> $ip<br/><b>score:</b> $winner<br/><hr/>" );

fclose( $file );

But, I have two wheels that each of them has its own result. I wish to duplicate the Ajax code so it will send the second wheel's result and print it to the HTML file using the PHP.

I have tried adding this after the first function:

function sendwinnertophp222(){
    var winner222 = $("#winner222").html();
    $.ajax({
        type: "POST",
        url: "getwinner.php",
        data: {
            "winner222": winner222
        },
        cache: false,
        success: function(data){ 
        //  alert(data);
        },
        error: function(err){
            alert(err);
        }
    });
}

And this to the PHP file:

$file = fopen("log.html", "a");
$ip=$_SERVER['REMOTE_ADDR'];
$winner = $_POST['winner'];
$winner222 = $_POST['winner222'];

//write the data
$time = date("H:i dS F");
fwrite($file, "<b>Time:</b> $time<br/><b>ip:</b> $ip<br/><b>score:</b> $winner<br/> $winner222<br/><hr/>" );

fclose( $file );

But it doesn't work.

1
  • I see that your data in the ajax call is wrong. data: { "winner222": winner222 }, The value is the one that should be around quotes, not the key. Commented Apr 5, 2016 at 11:21

1 Answer 1

2

Instead of Duplicating the same code twice you can simple increse the number of post variables :

function sendwinnertophp(){
    var winner = $("#winner").html();
    var winner_two = $("#winner222").html();
    $.ajax({
        type: "POST",
        url: "getwinner.php",
        data: {
            "winner": winner,
            "winner_two" : winner_two
        },
        cache: false,
        success: function(data){ 
        //  alert(data);
        },
        error: function(err){
            alert(err);
        }
    });
}

And in PHP File :

$file = fopen("log.html", "a");
$ip=$_SERVER['REMOTE_ADDR'];
$winner = $_POST['winner'];
$winner_two = $_POST['winner_two'];

//write the data
$time = date("H:i dS F");
fwrite($file, "<b>Time:</b> $time<br/><b>ip:</b> $ip<br/><b>score:</b> $winner<br/><hr/>" );
fwrite($file, "<b>Time:</b> $time<br/><b>ip:</b> $ip<br/><b>score:</b> $winner_two<br/><hr/>" );

fclose( $file );
Sign up to request clarification or add additional context in comments.

3 Comments

thank you ! your code did the job. only one thing. it is duplicating the resaults in the log file. see here: zeevm.co.il/rollet/log.html every time you press on "spin" it will show the resaults 4 times (insted of two)
If you check your network Panel you can see that there are two ajax request being called.
the log file is now cleared. i don't understand where are those two AJAX requests are coming from. if you look at the code, there is only one function with ajax in it

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.