I'm building a site where the admins need to be able to select multiple database entries (clients) via checkboxes. Once the relevant clients are selected, they can click on a button that redirects them to a page where an email can now be composed for only the selected clients. My checkboxes are set up as below:
<input name = 'checked_residents' value='1' id = '1' type='checkbox' />
<input name = 'checked_residents' value='2' id = '2' type='checkbox' />
<input name = 'checked_residents' value='3' id = '3' type='checkbox' />
I then have a button (id = 'mail_selected') which, when clicked, is able to construct an array with all the id's for the selected checkboxes. See code below:
$('#mail_selected').click(function(event) {
$(':checkbox[name="checked_residents"]:checked');
var selectedID = [];
$(':checkbox[name="checked_residents"]:checked').each (function () {
var tempStr = this.id;
selectedID.push(tempStr);
});
});
My problem is that I need to send the selectedID array to my php file "mailer_client.php", but I need to redirect to that page immediately, and populate my "Email To" input field with all emails from the respective ID's.
I realize that it may be difficult to understand what exactly I'm trying to do...I'm unsure myself. Please ask if anything regarding the problem is unclear.
If you change the name of your checkbox to be like an array, like this:
<input name = 'checked_residents[]' value='1' id = '1' type='checkbox' />
<input name = 'checked_residents[]' value='2' id = '2' type='checkbox' />
<input name = 'checked_residents[]' value='3' id = '3' type='checkbox' />
then you will receive them as an array in PHP in $_REQUEST['checked_residents'] and won't need your current code to construct the array yourself and send it.
You could then fill emails as follows:
foreach ($_REQUEST['checked_residents'] as $val) {
$email_to .= get_email_for_id($val) . ' , ';
}
Related
I have several forms on my page (via a loop), each have it's own submit button.
I want to use AJAX to get and POST the data to a .php. I need to know which form was submitted so I can get the corresponding values.
It's a simple cart application. This form allows a user to change the quantity of the items in the cart before checkout.
You see that there are two "Change Quantity" buttons (sumbit buttons) and these two fields are wrapped in a form tag.
The loop that encloses the form is :
while ($row = $query->fetch_assoc()) {
$item_code = $row['product_id'];
$item_name = $row['name'];
$item_price = $row['price'];
$item_qty = $row['qty'];
print"<tr>
<td>
<img src='http://localhost/store/images/products/$item_code.jpg'>
</td>
<td>$item_code</td>1
<td>$item_name</td>
<td>$$item_price</td>
<td>
<form method='post' action='/store/processing/view_cart.php' class='cart_view_ajax' id='form_id_$item_code'>
<input type='text' maxlength='2' value='$item_qty' id='qty'>
<input type='hidden' value = '$item_code' name='product_id' id='product_id'>
<div><input type='submit' id='submit' value='Change Quantity'></div>
</form>
</td>
<td>x</td>
</tr>";
}
The Question :
How do I know via jQuery that which form was submitted? Help me out...
According the given answers, I've managed to get the Quantity of the submitted form like this by jQuery. Here's the code :
$('form.cart_view_ajax').on('submit', function(e) {
e.preventDefault();
// start working
var form = $(this);
var form_id = form.attr('id');
console.log($('#'+form_id).children().eq(0).val());
return false;
});
You can just use the 'product_id' because it should be unique to each item
<input type='hidden' value = '$item_code' name='product_id' id='product_id'>
Or you can add an unique ID property to the form tag
<form method='post' action='/store/processing/view_cart.php' class='cart_view_ajax' id='item_$item_code'>
$('form#item_3')
You can add an unique id to the row so can manipulate it later on and you can access the form later on.
<tr id="row_$item_code">
$('tr#row_3 form')
You can add 'data' attributes to the form
<form method='post' action='/store/processing/view_cart.php' class='cart_view_ajax' id='item_$item_code' data-form_id="$item_code">
var form_id = $('form#item_3').data('form_id');
Can we see the jQuery code you are currently using?
You may use another hidden input field that contains an identifier for that special form. I think that would be the easiest way to do it.
I am having a bit of a problem, i am trying to store data with checkboxes, but i cant seem to figur it out. Right now i have made a script that can show multiple checkboxes all with the same name. So my question is now, how do i make the page where the data is sended to making them seperated? like:
<input type='checkbox' checked name='cpu-link[]' value='1' />
<input type='checkbox' checked name='cpu-link[]' value='2' />
<input type='checkbox' checked name='cpu-link[]' value='3' />
how can i get it stored for each checkbox in a new row in the mysql database?
so it would be like:
Data 1
Data 2
Data 3
And then i have a related qestion, how do i make so that the checkboxes that is not checked will get removed if they already is in the database.
so if it is like this:
<input type='checkbox' checked name='cpu-link[]' value='1' />
<input type='checkbox' name='cpu-link[]' value='2' />
<input type='checkbox' name='cpu-link[]' value='3' />
Then the row with the id 2 and 3 will get removed but the row with id 1 will get created or just stay if it already is in the database.
It is all a kind of a link between 2 tables.
I have a table with pc's in, and a table with hardware in.
then i have the table with the links in, so if i choose a hardware that needs to bee in a machine, it will create a row in the link table so that when i display the pc, it shows with that hardware oppotionity.
sorry for my bad english.
Thank You in Advance
$all_cpu_links = array(); // here is your array with all possible values of cpu-link
$previously_checked = array(); // array of values that are already in your database
$to_be_removed = array();
$to_be_inserted = array();
// It is better to use here here a prepared statement by the way
foreach ($_POST['cpu-link'] as $cpu_link)
{
if (!in_array($cpu_link, $previously_checked))
{
// validate $cpu_link here
mysql_query("INSERT INTO table (`cpu_link`) VALUES (".$cpu_link.");");
}
}
foreach ($all_cpu_links as $cpu_link)
{
if (!in_array($cpu_link, $_POST['cpu-link']) && in_array($cpu_link, $previously_checked))
$to_be_removed[] = $cpu_link;
}
mysql_query("DELETE FROM table WHERE `cpu_links` IN (".implode(' ,', $to_be_removed).");");
Below is a function that outputs a form with existing users from a database, with checkboxes and a submit button. The user choose one or several users to be removed and submits the form. What i'm working on right now is to implement a confirmation box in js ("Are you sure you want to remove..."), which shall contain the users that the user wants to remove.
By that reason I'm putting in the usernames in a hidden field that the js function will retrieve the username(s) from. The problem is that not only the usernames are put into the hidden input fields value attribute, but also several input fields as well (!?). If I hard code in for example 'username1,username2' in the attribute it works as it should, but not if I use the variable $users.
public function ShowUsers($userArray) {
$userIdArray = $userArray[0];
$userNameArray = $userArray[1];
$users = implode(",", $userNameArray);
echo $users; // username1,username2...
$nrOfUsers = count($userIdArray);
for ($i = 0; $i < $nrOfUsers; $i++) {
$users .= "<label for='$userIdArray[$i]'>
$userNameArray[$i]
<input type='checkbox' name='$this->_checkBox' value='$userIdArray[$i]' /><br/>
</label>";
}
$userList = "<div class='userList'>
<form id='form3' method='post' action=''>
<fieldset>
<p>Existing users</p>
$users
<input type='hidden' value='$users' />
<input type='submit' id='$this->_submitRemove' name='$this->_submitRemove' Value='Ta bort' />
</fieldset>
</form>
</div>";
return $userList;
}
The output is the following:
<input type='hidden' value='username1,username2<label for='47'>
username1
<input type='checkbox' name='check[]' value='47' /><br/>
</label><label for='50'>
username2
<input type='checkbox' name='check[]' value='50' /><br/>
</label>' />
I just don't get it why this happens? The content of $users (in this case 'username1' and 'username2') is there as expected, but why is the input and label tags put into the value attribute?
The problem is in your for loop. There you're appending all that HTML to the variable $users which already is $users == username1,username2. If you do echo $users; or even var_dump($users); just after you've finished the for loop, you'll see.
What are you trying to achieve in the for loop indeed? Did you rather mean
for ($i = 0; $i < $nrOfUsers; $i++) {
echo "<label for='$userIdArray[$i]'>
$userNameArray[$i]
<input type='checkbox' name='$this->_checkBox' value='$userIdArray[$i]' /><br/>
</label>";
}
I got the array of disabled text boxes from the database. When I click on edit button it should enable all the text boxes which are in array. But i could only enable the first text box. Here is the code.
code for edit button:
<input type="button" value="Edit" onclick="enable();" />
function enable()
function enable(){
document.getElementById("instance_name").disable = false;
document.getElementById("host_name").disable = false;
}
array:
$result=mysql_query("SELECT user_instance.instance_name, user_instance.host_name FROM
dba_account, user_instance WHERE dba_account.account_id = user_instance.account_id AND
dba_account.account_id = '$accid'");
while($note = mysql_fetch_array($result))
{
<inut type='text' name='instance_name' id='instance_name' disabled='disabled'
value='$note[instance_name]' size='25' />
<input type='text' name='host_name' id='host_name' disabled='disabled'
value='$note[host_name]' size='25' />
}
I am getting all the disabled textboxes, but i can not anable them all. Appreciate your help.
You're giving the same ID to multiple elements. Each ID can only be unique to one element, so once javascript finds the first ID, it stops. I'd give it a class="instance_name" instead and then get the elements by class instead. You could do something like this:
function enable(){
Array.prototype.forEach.call( document.getElementsByClassName('instance_name'),
function(element){
element.removeAttribute('disabled');
});
}
can we get the values of the radio buttons , only those which has been checked ,
cause i have to implement a form in which there are many radio buttons , so how can i get all the values which has been checked
what i have to do is
i have 13 subjects like : physics , math , biology .....
and the students can choose any number of subjects , and each subject will have two radio buttons one is A and the another is As ,
so how can i get the values of the radio buttons which has been checked?
using jquery
This would loop through all the radio elements in the document that have been checked:
$('input:radio:checked').each(function() {
var value = $(this).val();
// do something with radio or value
});
If you wanted to get the checked value of a particular radio group, you would do:
var value = $('input:radio[name=myradios]:checked').val();
So if your HTML was like this:
<input type='radio' name='myradios' value='1'>
<input type='radio' name='myradios' value='2' checked='checked'>
<input type='radio' name='myradios' value='3'>
value would be 2