0

Trying to pass 2 JS variables to my PHP page using AJAX. One variable is just time, and the other is a string of user input. I don't know how to get the user input from the modal form, I want to submit those variables in a GET and write them to a leaders.txt file.

            var d = new Date();
            var t = d.getTime();
            var rec = (t - time)/1000
            alert("Time = " + rec + " seconds!");
            var modal = document.getElementById("userName");
            $(modal).modal('show');
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET","index.php?time=" + rec, true);
            xmlhttp.send();

            <div class="modal fade in" id="userName" tabindex="-1" role="dialog"
                    aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal"
                                    aria-hidden="true">&times;</button>
                                    <h4 class="modal-title" id="myModalLabel">Add your name to the leader board!</h4>
                                </div>
                                <div class="modal-body">
                                        <form method="get" action="index.php" role="form">
                                            <div class="form-group">
                                                <label for="usr">Arcade Name:</label>
                                                <input type="text" class="form-control" id="usr">
                                            </div>
                                        <button type="submit" class="btn btn-default">Submit</button>
                                    </form>
                                </div>
                            </div><!-- /.modal-dialog -->
                        </div><!-- /.modal -->
                    </div>
                <?php
                    $time = $_GET["time"];
                    if($time != ""){
                        $name = $_GET["name"];
                        console.log($time + $name);
                        file_put_contents("leader.txt", $name.':'.$time."\n", FILE_APPEND);
                    }
                ?>

1 Answer 1

1

Change your code in code xmlhttp.open("GET","index.php?time=" + rec, true); to xmlhttp.open("GET","index.php?time=" + rec+"&modal="+modal,true);

and get this variable in index.php $_GET['model'].

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.