1

I'm making a php page that inserts values written by the user into an MySql table. I'm trying to get the textbox values sent to the php code portion and can't do it.

I currently have this php function:

function insertion($Alias,$Password,$Nom,$Adresse,$Telephone,$CatPrefere){
       try{
        $insert = $mybd->prepare("CALL insertClients(?,?,?,?,?,?,?)",array(PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY));
        $insert->bindParam(1,$Alias);
        $insert->bindParam(2,$Password);
        $insert->bindParam(3,$Nom);
        $insert->bindParam(4,$Adresse);
        $insert->bindParam(5,$Telephone);
        $insert->bindParam(6,0);
        $insert->bindParam(7,$CatPrefere);
        $insert->execute();
       }
       catch(PDOException $e){ 
       echo('Erreur insertion dans table client: '.$e->getMessage());
       exit();
       }

And this Javascript function (notice the last line here, i call my php function):

function inscription() {
    var Alias = document.getElementById("alias").value;
    var Password = document.getElementById("password").value;
    var Nom = document.getElementById("nom").value;
    var Adresse = document.getElementById("adresse").value;
    var Telephone = document.getElementById("telephone").value;
    var Categories = document.getElementById("categorie");
    var CatPrefere = Categories.options[Categories.selectedIndex].value;
    <?php insertion(Alias,Password,Nom,Adresse,Telephone,CatPrefere);?>
}

How do i send all thoses variables to my php function?

If this is all correct then something else might not work in my spaghetti code.

Also i'm working on a single .php file named Inscription.php.

Void Dark
  • 63
  • 5
  • PHP runs on the server in order to generate the page. Javascript runs inside your user's browser (the client side). The only way to use Javascript variables is to pass them with a new request (likely using AJAX) – Cfreak Apr 30 '18 at 03:50
  • 1
    You might want to learn [ajax](https://www.w3schools.com/xml/ajax_intro.asp) to do that. [Here](https://stackoverflow.com/questions/13791474/communication-between-php-and-javascript) is a similar question – Chaitya Shah Apr 30 '18 at 03:50
  • If you can or do use jQuery this would be considerably easier. Is that the case? – tadman Apr 30 '18 at 05:46
  • PDO also supports named placeholders so you can pass in an associative array to the `execute` call and do it all in one shot. This is often a lot less messy than you have here because if you remove an argument you don't need to renumber a bunch of things. – tadman Apr 30 '18 at 05:47

1 Answers1

0

I think that is not possible. What you can do it is change the URL to get values in $_GET.

Look that stack overflow topic: send javaScript variable to php variable