Thanks John! Sounds good. My knowledge in JS and jQuery is a bit limited so I'm going to post here more of my code in hope you can help me out...
Here's the portion of the code where you can see how the variables are passed to the javascript for further processing :
PHP Code:
// form method=post, action=javascript:insert()
$users_result = $database->retrieveAllTableRecords("users");
$num_of_users = $users_result->rowCount();
for ($i = 1; $i <= $num_of_users; $i++) {
$users_row = $users_result->fetch(PDO::FETCH_ASSOC);
echo "<li><input type='checkbox' name='username_".$i."' id='username_".$i."' value='' />".$users_row['full_name']."</li>";
}
// form submit
Now here is the WHOLE JS (I've tried to add your solution with the post method, but I couldn't make it to work...) file which supposed to retrieve the submitted form variables and pass them to the insert.php :
Code javascript:
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}
var http = createObject();
/* -------------------------- */
/* INSERT */
/* -------------------------- */
var nocache = 0;
function insert() {
// Optional: Show a waiting message in the layer with ID login_response
document.getElementById('insert_response').innerHTML = "<div class='info'>Working...</div>"
var insert_what = encodeURI(document.getElementById('insert_what').value);
if (insert_what == "assign_user") {
var num_of_users = parseInt( $("#num_of_users").val(), 10 );
// FROM SITEPOINT
i = 0;
// we'll use an object to store the data we want to submit
data = { "num_of_users": num_of_users, "users": [] };
usernameFields = $(".username_field"); //assuming your username fields have been given a class of username-field
usernameFields.each(function() {
var field = $(this);
data.users.push( { field.attr("id"):( field.attr("checked") ? true : false );
});
// FROM SITEPOINT end
}
// Set te random number to add to URL request
nocache = Math.random();
// FROM SITEPOINT
$.post("../ajax/insert.php", data, function(data, textStatus, jqXHR) {
//do something with returned data and/or textStatus
http.onreadystatechange = insertReply;
http.send(null);
});
// FROM SITEPOINT end
}
function insertReply() {
var response = http.responseText;
// else if login is ok show a message: "Site added+ site URL".
if(response == "Changes have been saved!") {
document.getElementById('insert_response').innerHTML = '<div class="success">'+response+'</div>';
}
else{
document.getElementById('insert_response').innerHTML = '<div class="error">'+response+'</div>';
}
}
and, just-in-case here is how I tried to retrieve the passed values at insert.php:
PHP Code:
$num_of_users = $_POST['num_of_users'];
$username_2 = $_POST['username_2']; // just to check if it is passed...
if ($username_2 == "true") { $username_2 = "checked!!!"; }
echo "Num of users : ".$num_of_users."<br />Username 2 :".$username_2;
I believe it's not too complicated, even with my JS 'knowledge' but I also believe I need some instructions here and there... Can you please advise me where am I wrong with this code? Thank you in advance.
Bookmarks