0

i want to get all users with a specific name and this name is in a javascript variable and this code does not work how should i do it?

<table id="table">
        <tr>
        <td>id</td>
        <td>name</td>
        <td>info</td>
        </tr>
    </table>
<script>
        var table = document.getElementById("table");
$.ajax({ 
        type:"POST", 
        url:"process.php", 
        data:{name:"mike"}
        });
</script>
<?php include 'process.php'; ?>
<?php foreach ($users as $user) : ?>
<script>
var row = table.insertRow(1);
  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);
  var cell3 = row.insertCell(2);
  cell1.innerHTML="<?= $user->id ?>";
  cell2.innerHTML="<?= $user->name ?>";
  cell3.innerHTML="<?= $user->info ?>";
</script>
<?php endforeach; ?>

php

$name = $_POST["name"];
$database = [ 
    'host'   => 'localhost', 
    'dbname' => 'dbname', 
    'user'   => 'user', 
    'pass'   => '' 
];
try { 
    $db = new PDO("mysql:host={$database['host']};dbname={$database['dbname']}", $database['user'], $database['pass']); 
} catch (PDOException $e) { 
    die("An error happend, Error: " . $e->getMessage()); 
}
function getAllUsers(){
    global $db;
    $sql = "SELECT * FROM users WHERE $name = name";
    $stmt = $db->prepare($sql);
    $stmt->execute();
    return $stmt->fetchAll(PDO::FETCH_OBJ);
}
$users = getAllUsers();

the error is "Undefined array key "name" in process.php on line 2" could you help me to send my data to my php file and get a query based on that

Meysam
  • 37
  • 1
  • 8
  • 2
    Do you really mean to get the column name from the user? Don't you mean `name = $name` (which has a bunch of other issues, like the value not being quoted and being completely open for SQL injections)? Please read [How to prevent SQL injection in PHP](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – M. Eriksson Jul 26 '21 at 09:16
  • 1
    Change $sql = "SELECT * FROM users WHERE $name = name"; to $sql = "SELECT * FROM users WHERE name = $name"; – Grumpy Jul 26 '21 at 09:17
  • You're making an ajax call to `process.php` (which I assume is the PHP code?) where you pass the parameter `name`, but why are you also including that PHP file? At that point, there's no `$_POST['name']` (since you haven't posted anything to that page, just loaded it). What is the purpose of the Ajax call? It will select data from the database but not do anything with it. An Ajax call is a completely separate request to the server. – M. Eriksson Jul 26 '21 at 09:21
  • Magnus Eriksson . first a wanted to get the data from database using ajax but it issued some problem a want to send name to php and get all users with that name and show it in table how should i do it? – Meysam Jul 26 '21 at 09:36
  • After making AJAX call fetch data from it, there is no point in including the php file here. – Mohammed Khurram Jul 26 '21 at 11:52
  • I tried but its not working for what i want to do can you help with that? – Meysam Jul 26 '21 at 11:55

1 Answers1

0

I would achieve what you want with...

html

<table id="table">
        <tr>
        <td>id</td>
        <td>name</td>
        <td>info</td>
        </tr>
    </table>
<div id="scriptsforTable"> </div>
<script>
        $.ajax({ 
    type:"POST", 
    url:"process.php", 
    data:{name:"mike"},
    success:function(data) {
        $("scriptsforTable").html(data); 
}
    });
</script>

process.php

$name = $_POST["name"];
$database = [ 
    'host'   => 'localhost', 
    'dbname' => 'dbname', 
    'user'   => 'user', 
    'pass'   => '' 
];
try { 
    $db = new PDO("mysql:host={$database['host']};dbname={$database['dbname']}", $database['user'], $database['pass']); 
} catch (PDOException $e) { 
    die("An error happend, Error: " . $e->getMessage()); 
}
function getAllUsers(){
    global $db;
    $sql = "SELECT * FROM users WHERE $name = name";
    $stmt = $db->prepare($sql);
    $stmt->execute();
    return $stmt->fetchAll(PDO::FETCH_OBJ);
}
$users = getAllUsers();
echo '<script>var table = document.getElementById("table");</script>';
foreach ($users as $user) :
echo '<script>
var row = table.insertRow(1);
  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);
  var cell3 = row.insertCell(2);
  cell1.innerHTML='.$user->id.';
  cell2.innerHTML='.$user->name.';
  cell3.innerHTML='.$user->info.';
</script>';
endforeach; 

There might be some javascript errors with this, you can sort it out easily.

Mohammed Khurram
  • 616
  • 1
  • 7
  • 14