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');
});
}
Related
Maybe this is a stupid question, but I was wondering if I could get information passed with a form into a for-loop.
The problem is as follows:
The passing of the information works, though he passes only for the last loop. For example if I click the submit button for loop 2 ($i = 2). The command $_POST['titel'] will only remember the information in last loop ($i = $numact-1) and not loop 2 ($i = 2).
for example if titel[0] = test0, titel[1] = test1, titel[2] = test2. and I click the submit button below titel[0] he passes the information from titel[2]. Is there an easy way to get around this?
I have the following code (For the sake of simplicity I shortened it);
<?php
for ($i = 0; $i <= $numact-1; $i++) {
echo "<tr><td width='150'>
<input type='text' name='titel' value='$titel[$i]' />
</td></tr>
<tr><td><input type='submit' name='submitreg' value='Toon activiteit'/>
</td></tr>";
}
?>
You're using the same parameter name over and over again (name=titel - you had a type and probably meant to write as 'title') so when the form submits - only one value will be passed.
You can easily fix that by passing the parameters as:
...
echo "<tr><td width='150'>
<input type='text' name='titel$i' value='$titel[$i]' />
</td></tr>
<tr><td><input type='submit' name='submitreg' value='Toon activiteit'/>
</td></tr>";
...
and reading it on the other side as title0, title1 etc.
As seen in this post, you can pass in an array simply by putting brackets around the index. PHP will reconstruct the array for you.
Also, you probably want your submit at the end of your table, not after each row. Your code should look something like this:
<?php
for ($i = 0; $i <= $numact-1; $i++) {
echo "<tr><td width='150'>
<input type='text' name='titel['.$i.']' value='$titel[$i]' />
</td></tr><tr><td><input type='submit' name='submitreg' value='Toon activiteit'/>
</td></tr>";
}
?>
The advantage of this approach is that it's more extensible, should you choose to use different values. However, Edson Junior's approach is slightly simpler.
You can change the name attribute to name=titel[] so PHP will automatically transform it to an array. It is not necessary to specify the index inside title[].
For example:
<input type="text" name="titel[]" value="x" />
<input type="text" name="titel[]" value="y" />
<input type="text" name="titel[]" value="z" />
When you submit the form, PHP will transform it to
"title" = array (
0 => "x",
1 => "y",
2 => "z"
);
In case you need to check what button was clicked, you could change the inputs type="submit" to <input type="button" name="submitreg" class="submitreg" id="whatever_you_want_but_has_to_be_unique" />.
Outside the loop, add another input wich will have the value of what button was pressed.
<input type="hidden" id="buttonPressed" name="buttonPressed" value="" />
Then add this code (you need to import jQuery library to the page so it understands the code below, wich is jQuery, here is the Docs.
<script type="text/javascript">
$(function(){
$("button.submitreg").live("click", function(){
var buttonPressed = $(this).attr("id");
("input#buttonPressed").val(buttonPressed);
});
}
</script>
All you need to do is get the buttonPressed value and you'll know what button was pressed.
Hi i am having results and its contains a checkbox as <input type='checkbox' name="ChkS[]" value='<? echo $rs->id?>'>
I have a print out list option in the footer as <input type="button" value="Show Printable List" class="butten" onClick="openPrint()">
Its opening the printable list in new pop up window.
function openPrint() {
window.open("<?= $_SERVER["PHP_SELF"]; ?>?<?=$qstring?>&print=true","","width=1024,height=600,menubar=yes,resizable=yes, scrollbars=yes");
}
I am in need to use the above select checkbox and it should show the selected print list in the pop up ?
I hope it should not be problem any1 to understand it. Any help ?
Javascript Modification:
var s = "";
function changeval(value) {
s = value;
}
function openPrint() {
alert(s);
window.open("<?= $_SERVER["PHP_SELF"]; ?>?<?=$qstring?>&print=true","","width=1024,height=600,menubar=yes,resizable=yes, scrollbars=yes");
}
HTML Modification:
<input type='checkbox' name="ChkS[]" value='<? echo $rs->id?>' onchange="changeval(this.value)" />
What I have done?
Created an Global Variable s.
Attached or Bind onchange event on checkbox and set the value of Variable s according.
Now, You have variable s with value, you just have to use it in any function.
Currently you have defined button onclick event , instead of this set onclick event on
checkbox.
<input type='checkbox' name="ChkS[]" value='<? echo $rs->id?>' onClick="openPrint();">
Hope this will help you to solve your problem.
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) . ' , ';
}
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>";
}
This is the code for the php file containing the form with radio buttons:
<?php
while($r7 = mysql_fetch_array($rt3)){
$name=$r7["name"];
$s=$r7["question"];
echo "
<div>$s</div>
<form name='Tester' method='post' onSubmit='return RegValidate(this);' action='quest.php' >
<input type='radio' name='w1' value='1'>1<br>
<input type='radio' name='w1' value='2'>2<br>
<input type='radio' name='w1' value='3'>3<br>
";
}
echo" <input name='Submit' value='Tester' type='submit' />
</form>";
?>
The $name and $q are derived from the database using a mysql query. This works fine.
I have used the following javascript code so far:
function RegValidate(Tester)
{
if($('#w1').not(':checked'))
{
alert('Radio not checked');
return false;
}
}
But with this code, I continue to get the error message, and I am not able to move on even though I have selected a radio button.
The while loop produces several sets of radio buttons, as $q, gets the question from the database and posts it on the page along with 3 radio buttons. Each question $q, has the 3 radio buttons in the above, hence the reason for the $name.
How do I validate this form with javascript. NB. The form will only contain radio buttons.
function RegValidate(Tester) {
if ($(":radio[name=w1]:checked").toArray().length == 0) {
alert('nothing selected');
return false;
}
}
FIDDLE
you have an error, your input doesn't have ID or CLASS, so add to all of the input radio class='w1' and try the next code:
if ($(".w1 option:selected").length<1)
alert ('nothing selected');
also take a look to this post: Check if option is selected with jQuery, if not select a default
do these things:
<input type='radio' class='w1' name='w1' value='1'>1<br>
<input type='radio' class='w1' name='w1' value='2'>2<br>
<input type='radio' class='w1' name='w1' value='3'>3<br>
And in js:
Latest edit:
if ( $(':radio[class=w1]').attr('checked') != 'checked' ) {
alert('Radio not checked');
}