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!

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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 ?
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

3 Comments

use it on onclick then u can get dynamic for each row.
where should these 2 block of codes reside, if the file anywhere or within the modal ?
in u ".js" or ur "wiew file" file in which u using to do the ajax call

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.