-2

Hi guys today i have tried to send parameters using AJAX to PHP with GET method, anyways everything is working good except that the INSERT query ain't be executed, so here is my Table with all DATA from my Database Tables :

<?php 
        if (isset($_GET['engins']) && !empty($_GET['engins'])) {
            # code...
            $engin=$_GET['engins'];
            $query="SELECT * FROM `engin` WHERE `genre_eng` LIKE '".$engin."'";
            if ($result=mysql_query($query)) {
                # code...
     ?>

    <center>
        <table border="3" id="myTable">
            <thead>
                <tr>
                    <th>Matricules des Engins</th>
                    <th>Numéro des Engins</th>
                    <th>Marque des Engins</th>
                    <th>Type des Engins</th>
                    <th>Genre des Engins</th>
                    <th>Modele des Engins</th>
                    <th>Puissance des Engins</th>
                    <th>P.T.C.A des Engins</th>
                    <th>Poids vide</th>
                    <th>Gestion</th>
                </tr>
            </thead>
            <tbody id="p_scents">
                <?php 
                    while ($row=mysql_fetch_assoc($result)) :
                ?>
                <tr>
                    <td><?= $row['mat_eng']; ?></td>
                    <td><?= $row['num_serie']; ?></td>
                    <td><?= $row['marque_eng']; ?></td>
                    <td><?= $row['type_eng']; ?></td>
                    <td><?= $row['genre_eng']; ?></td>
                    <td><?= $row['modele_eng']; ?></td>
                    <td><?= $row['puissance_eng']; ?></td>
                    <td><?= $row['p.t..a.c']; ?></td>
                    <td><?= $row['poids vide']; ?></td>
                    <td><a href="delete.php?del=true&engins=<?= $row['genre_eng']; ?>" title="delete">Supprimer</a></td>
                </tr>
                <?php endwhile; ?>
                <tr>
                    <td><input type="text" name="mat_eng" value="" placeholder="Matricules des Engins"></td>
                    <td><input type="text" name="num_serie" value="" placeholder="Numéro des Engins"></td>
                    <td><input type="text" name="marque_eng" value="" placeholder="Marque des Engins"></td>
                    <td><input type="text" name="type_eng" value="" placeholder="Type des Engins"></td>
                    <td><input type="text" name="genre_eng" value="" placeholder="Genre des Engins"></td>
                    <td><input type="text" name="modele_eng" value="" placeholder="Modele des Engins"></td>
                    <td><input type="text" name="puissance_eng" value="" placeholder="Puissance des Engins"></td>
                    <td><input type="text" name="p.t.a.c" value="" placeholder="P.T.C.A des Engins"></td>
                    <td><input type="text" name="poids_vide" value="" placeholder="Poids vide"></td>
                    <td><button title="add" onclick="ajax()">Ajouter</button></td>
                </tr>
            </tbody>
        </table>
    </center>
    <?php }} ?>

and This is my normal Ajax script :

<script language="javascript">

function getXHR() { 
    var resultat=null; 
    try {resultat= new XMLHttpRequest();} 
    catch (Error) 
    { 
        try {resultat= new ActiveXObject("Msxml2.XMLHTTP");} 
        catch (Error) 
        { 
            try {resultat= new ActiveXObject("Microsoft.XMLHTTP");} 
            catch (Error) {resultat= null;} 
        } 
    } 
    return resultat; 
} 

function ajax(){

    var mat_eng=document.getElementsByTagName('mat_eng').value;
    var num_serie=document.getElementsByTagName('num_serie').value;
    var marque_eng=document.getElementsByTagName('marque_eng').value;
    var type_eng=document.getElementsByTagName('type_eng').value;
    var genre_eng=document.getElementsByTagName('genre_eng').value;
    var modele_eng=document.getElementsByTagName('modele_eng').value;
    var puissance_eng=document.getElementsByTagName('puissance_eng').value;
    var ptac=document.getElementsByTagName('ptac').value;
    var poids_vide=document.getElementsByTagName('poids_vide').value;

    var obXHR = new getXHR();
    function traitement(){
        if(obXHR.readyState==4){
            var result = obXHR.responseText;
            document.getElementById('p_scents').innerHTML = result;
        }
    }

    obXHR.onreadystatechange=traitement;
    obXHR.open("get",
        "add.php?mat_eng="+mat_eng+"&num_serie="+num_serie+"&marque_eng="+marque_eng+"&type_eng="+type_eng+"&genre_eng="+genre_eng+"&modele_eng="+modele_eng+"&puissance_eng="+puissance_eng+"&ptac="+ptac+"&poids="+poids_vide
        ,true);
    obXHR.send(null);
} 
</script> 

One Other Javascript function() that i think it cause the error even if it can't be run without the onClick() method :

<script type="text/javascript">
    function subthis(){
        document.getElementById('myForm').submit();
    }
</script>

any help is accepted with pleasure, Thanks lot !

  • Have you tried to execute your insert statement with example values? If so, did it work? – Reporter Apr 28 '14 at 12:33
  • 2
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Apr 28 '14 at 12:34
  • @Quentin i know bro thanks, it's just a simple example i'm trying to just to study how to do things with `AJAX`, and after that of course i will use `mysqli` or `PDO` – Jeffery ThaGintoki Apr 28 '14 at 12:36
  • Then take the database stuff out entirely. It only makes the PHP more complicated and thus harder to see what the Ajax side of things is doing. – Quentin Apr 28 '14 at 12:38
  • I tried that too and used just a normal echo in my `php file`, but nothing was returned, it's like that after i click on the button the page refresh and don't send any request, or should i change the `$_GET` with `$_REQUEST`? – Jeffery ThaGintoki Apr 28 '14 at 12:41

2 Answers2

1

First of all I'd like to say there are decent libraries that handle HTTPRequests for you.

Have a look at jQuery | $.ajax function.

This not only reduces the lines you code but also helps with the readability.

As Quentin stated you are also vulnerable to SQL injection. Use prepared statements.

$.ajax({
       type: "GET", // GET, POST , PUT, PATCH
       url: "http://example.com/file.php",
       data: {engins:"exampleengine"},               
       success: function(data) { // Data will be your HTML code returned from the PHP script.
             // Process data object
       }

This piece of code should handle the request to the server, when given the correct information for engins.

When succesfully handled, the data object will be filled with your HTML code. Open the console of firefox or chrome to check if the request is actually send.

Next step is to add the HTML code to the body of the page.

function succes(data){
  $('body').append(data); // If data is HTML code it will be appended to the DOM tree
}
Jens
  • 1,132
  • 7
  • 14
  • Thanks dude, I know it's going to work with jQuery but i wonder why it is not the case with simple javascript methods? – Jeffery ThaGintoki Apr 28 '14 at 13:07
  • There's isn't anything wrong with the php code on first sight. You probably made some typo's in the javascript code. Write less, do more is what you should aim for. The less you write, less mistakes you can make. I'd like to help you out but there's no way of telling now if I can't see it with my own eyes. – Jens Apr 28 '14 at 13:09
0

You are using getElementsByTagName() to set the parameters values witch it return a table of values not a single value .

you might need to update your code to :

var mat_eng=document.getElementsByTagName('mat_eng')[0].value;
// ...
Abdou Tahiri
  • 4,338
  • 5
  • 25
  • 38