Home » Php » How do I send a value from Javascript to PHP that will not be changed?

How do I send a value from Javascript to PHP that will not be changed?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have function that is running multiple times and executing a PHP file. I do, however, want to make sure the functions does not interfere with each other.

for(int i = 0; i<5; i++){
    functionName(i)
}

function functionName(number){
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
        }
    };
    var PageToSendTo = "phpFile.php?";
    var MyVariable = number;
    var VariablePlaceholder = "name=";
    var UrlToSend = PageToSendTo + VariablePlaceholder + MyVariable;
    xhttp.open("GET", UrlToSend, true);
    xhttp.send();
}

This is how my code looks so far, how do I change it so that the next iteration of the function does not effect the previous one?

phpFile.php

<?php
require '../notWebsite/dbh.php';
session_start();

$variable = $_GET['name'];

$sqlInsertClass = "INSERT INTO class (className) VALUES (?)";
$stmt = mysqli_stmt_init($conn);

if(!mysqli_stmt_prepare($stmt, $sqlInsertClass)) {
    header("Location: ../Website.php?error=InsertError");
    exit();

} else {
    mysqli_stmt_bind_param($stmt, "s", $variable);
    mysqli_stmt_execute($stmt);
    exit();
}

mysqli_stmt_close($stmt);
mysqli_close($conn);

?>
How to&Answers: