1

I have a php loop that writes hundrends of records, I need to have a button on each line to trigger a bootstrap modal that will allow to edit this specific record. can't really sent the line ID (it's a user ID) via GET since the URL doesn't change, or by POST since the page doesn't refresh ... I tried with AJAX but could seem to figure it out.

in my file the php variable is $userID= some number; ... which changes at every line

so every line refers to the same modal in which i added a data-id (or is there any other way to pass tha value to my modal?)

<a href="#" class="btn btn-success"
data-toggle="modal"
data-target="#editUser"
id="theuser"
data-id="<?php echo $userID;?>"> 
Update
</a>

How can I retrieve $userId in the modal so I can do an Mysql query on it ? (i understand there is a seperation between the server side aspect of php and the fact the data-id is only browser based ... but there must be a way to generate dynamically content using php for a modal ?

thanks in advance!

amphetamachine
  • 27,620
  • 12
  • 60
  • 72
zefrank
  • 53
  • 2
  • 6

3 Answers3

1

You can send user_id as param in URL <a href="/your_route?user_id_for_query=<?php echo $userID;?>" specify_method> and than in your action for that route you could access it as GET or POST param.

RichardBernards
  • 3,146
  • 1
  • 22
  • 30
ClassyPimp
  • 715
  • 7
  • 20
0

You'd have to access it through jQuery and then run an ajax request to a php file that would run your sql query.

var userID = jQuery('#theuser').data('id');

The above fetches the data attribute data-id which will allow you to use it in an ajax request.

jQuery.ajax({
    type: "POST",
    url: "yourscript.php",
    data: {userID: userID},
    timeout: 30000,
    datatype: 'json'
}).success(function (data) {
    console.log(data);
})

You're absolutely right about the server and client being separate. You need to parse the client side data back to the server to be processed as you require.

Darren
  • 13,050
  • 4
  • 41
  • 79
  • I think i get it but not sure about the where i need to put all this. the modal needs to bring up a form prefilled with data so we can edit a user credentials. assuming my base file is index.php , where would i put this code ? inside the modal ? – zefrank Feb 10 '15 at 03:50
0

Try this : You can get the value of user_id from the given code

var userID = $('#theuser').attr('data-id');

the pass the userID IN AJAX call

$.ajax({
    type: "POST",
    url: "yourscript.php",
    data: {userID: userID},
    timeout: 30000,
    datatype: 'json'
}).success(function (data) {
    console.log(data);
})

i think it will meet your needs. Thank You