0

I'm having troubles displaying a value in an input field. I did this in the past, and I haven't got a clue where my code goes wrong.

I have an input field with id="input" and a button with id="button". This is my jquery code:

$("#button").click(function() {

      var uid = <?php echo $user['uid']; ?>;

      $.ajax({
          url: "php/fetchUserData.php",
          method: "POST",
          data: {
            uid: uid
          },
          dataType: "json",
          success: function(text) {    
            $("#input).val(text.bedrijfsnaam);
          }    
      });

});

And here is the code on of the php/fetchUserData.php file:

<?php 

include_once 'dbc.php';

if($_POST){

    $uid = $_POST['uid'];

    $sql = "SELECT * FROM users WHERE uid = '$uid'";
    $query = mysqli_query($dbc, $sql);
    $result = mysqli_fetch_assoc($query);

    echo json_encode($result);

}

?>

UPDATE:

  • var_dump($result) does displays the associative array.
  • console.log(text) gives no result.
  • if I change dataType to text and echo out $result['bedrijfsnaam'] instead of json_encode($result) all goed well. The problem is that I want to load more than just the bedrijfsnaam (= company name).

UPDATE 2:

If I use the very same code but with another table in the database it does works. I really don't have a clue what can be the problem here... I've been searching what could be the matter with the users table, and I notice cardinality is 0, although there are 4 rows in the table. In the other tables of the database, the cardinality value represents the number of rows. Could that have anything to do with this problem?

UPDATE 3:

Instead of the query:

$sql = "SELECT * FROM users WHERE uid = '$uid'";

I tried:

$sql = "SELECT bedrijfsnaam FROM users WHERE uid = '$uid'";

And it worked! Then I started adding column names, and all went well until a certain column: land (meaning country) a varchar column just like many others in the table.

What could be the reason this particular column causes the error to happen?

I know this became a phpmyadmin question instead of a php or jquery question. Should the question be moved to the sql part of the forum?

auurk
  • 47
  • 8
  • have you tried `console.log(text)` what does it print in `console` – Muhammad Omer Aslam Dec 28 '17 at 21:45
  • 2
    You're missing the closing quote in `$("#input")`. – Barmar Dec 28 '17 at 21:56
  • 2
    Your code is vulnerable to SQL injection. See [How can I prevent SQL injection in PHP](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Barmar Dec 28 '17 at 21:58
  • 1
    Also is `$user['uid']` a number or string? – charlietfl Dec 28 '17 at 22:01
  • When assigning PHP variable values to JS, it's always best to use `json_encode()` to ensure the format is valid, ie `var uid = = json_encode($user['uid']) ?>;` – Phil Dec 28 '17 at 23:40
  • Also, add an error handler to your `$.ajax` call – Phil Dec 28 '17 at 23:43
  • it's normal you get only 0 cardinality, you select from uid, any users have the same uid? if yes you should have multi rows, if not you have one line result... so when you encode it you have 0 : value of results obvious.... – Robin Dec 29 '17 at 08:19
  • and about you update1 your main question relative to the post is solved because you say it's working, prefer to open an other question ? – Robin Dec 29 '17 at 08:31

3 Answers3

0

I don't know anything about PHP and I don't think it matters. I think you got most part right. In success of your ajax request, set the text value of the input field.

$.ajax({
    url:"php/fetchUserData.php",
    method: "POST",
    data:{uid:uid},
    dataType:"json",  
    success:function(text){
        $("id='button'").text(text.bedrijfsnaam);
    }
 });
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
ntstha
  • 1,187
  • 4
  • 23
  • 41
  • Thanks, I actually had that. I changed it into an alert to be check if that would work and forgot to change it again) – auurk Dec 28 '17 at 21:41
0

Assuming this is your actual code, your issue is likely stemming from not actually referencing and updating a field.

Something like this should be what you need:

$("#input").val(text.bedrijfsnaam)
jpadd009
  • 45
  • 4
  • this is already the part of the code what are you suggesting to change? – Muhammad Omer Aslam Dec 28 '17 at 21:44
  • 1
    OP had an alert() when I added my answer. In any case, I managed to not catch it before, but OP may need to parse the server's response using "JSON.parse()" in your "success" before you can update the field, otherwise, it will break. – jpadd009 Dec 28 '17 at 21:49
  • @MuhammadOmerAslam ..where in the code? I don't find it. – SherylHohman Dec 28 '17 at 21:49
  • 1
    @jpadd009 ah ok that must be the case, @SherylHohman it is in the `success` callback function but the OP edited it , it wasnt there before when this answer was posted or wouldnt have noticed – Muhammad Omer Aslam Dec 28 '17 at 21:53
  • 2
    `dataType:"json"` takes care of `JSON.parse()` and it is done internally but will also go to error callback if invalid json – charlietfl Dec 28 '17 at 21:56
0

$.ajax({
    url:"php/fetchUserData.php",
    method: "POST",
    data:{uid:uid},
    dataType:"json",  
    success:function(text){
        $('#input').val(text[0]);
    }

});

hmtl maybe better works than .val

You're wrong with your jquery selection of your div: you're missing an " in your code.

hope it will work

Robin
  • 126
  • 9