HTML forms, add area on click - php

I'm not sure how to ask what I'm looking for to search how-tos. I am building a form and one of the entries requires users to list an appliance, its voltage, watts, amps, phase, etc.
I'd like a simple row with "X" columns providing the text areas for one appliance and then the ability to click a link to 'add another appliance' using jquery/html.
I like using placeholder text to save space on the page. I can get all this set up just fine for a single entry like 'name' however I don't know how to implement an 'add entry' row. All of the data is stored via PHP in MySQL.
So A: What is the name of this type of form section. B: What is it called when we want to let the user add a row to this section?
I love making things harder than they really are. It's my specialty. I guess :)
EDIT: http://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_submit
Using this format with 5 columns per entry (though it will all be on one line/row) I'd like to have an "add entry" link which generates a new blank entry option.
#elecNeeds input[type=text], textarea {
font-size: 12px;
font-style: italic;
width: 15%;
height: 20px;
padding: 10px;
color: #212323;
background: #E3E3E3;
background: rgba(255, 255, 255, 0.5);
border: 2px #000 solid;
margin-bottom: 10px;
position: relative;
behavior: url(/js/PIE.htc);
}
<div id="elecNeeds">
<input type="text" name="appliance" placeholder="Type of Equipment">
<input type="text" name="voltage" placeholder="Voltage">
<input type="text" name="watts" placeholder="Watts">
<input type="text" name="amps" placeholder="Phase">
<input type="text" name="notes" placeholder="Notes">
<br /> Add an appliance
</div>

I don't know what's it called, but you probably want this - http://jsfiddle.net/uPWkf/1/
<form method="post" action="#" id="myForm">
<div id="nameFields">
<label>Watt <input type="text" name="watt0" /></label>
<label>Volt <input type="text" name="volt0" /></label>
<label>Amp <input type="text" name="amp0" /></label><br/><br />
</div>
<input type="submit" value="submit" id="submit" />
</form>
Add New Row
and the JS
var i = 1;
$("#addRow").click(function() {
$("#nameFields").append('<label>Watt <input type="text" name="watt' + i + '" /></label><label>Volt <input type="text" name="volt' + i + '" /></label><label>Amp <input type="text" name="amp' + i + '" /></label><br/><br />');
i++;
});
$('#myForm').submit(function() {
var values = $('#myForm').serialize();
alert(values);
});

I think You need to use $(selector).append('<code>'); function. For example:
Add
<table class="my_fuits">
<tr>
<td>Fruit</td>
<td><input type="text" name="fuits[]" /></td>
</tr>
</table>
and js(jQuery) code:
$(document).ready(function(){
// add one more row
$(".add").live('click',function(){
$(".my_fuits").append('<tr><td>Fruit '+$(".my_fruits input").length+'</td><td><input type="text" name="fuits[]" />[X]</td></tr>');
return false;
});
// remove row
$(".remove").live('click',function(){
$(this).parent().parent().remove();
return false;
});
});

Related

Ajax Ignoring SQL

I have other functions just like this that work, however when I click, the Ajax gets posted but my page would originally refresh, until I added event.preventDefault. I have been stumped for hours and I cant see the problem..
Here is my AJAX:
$(function(){
$(document).on('click','.addnewproject',function(){
event.preventDefault();
var curaid= $(this).attr('id');
var $ele = $(this);
$.ajax({
type:'POST',
url:'components/sql/insertproject.php',
data:$("#addnewprojecttable").serialize(),
success:
function(data){
$(".image-list" + curaid).append(
'<li id="projectfolder' + curaid + '">\n\
<img width="35px" onclick="openproject('+ curaid +')" style="cursor: pointer;" src="img/folder.png" /> \n\
<p>Project ' + curaid + '</p> \n\
');
}
});
});
});
My HTML:
<form id="addnewprojecttable" method="post" >
<table>
<tr>
<td>
<input style="display:none;" name="username" id="username" value="<?php echo "" . $_SESSION['username']; ?>" type="text"/>
<input style="float:left; width:100%; height:20px; margin-bottom: 20px;" name="projectname" id="projectname" type="text" placeholder="projectname"/>
<input style="float:left; width:100%; height:20px; margin-bottom: 20px;" name="contactname" id="contactname" type="text" placeholder="contactname"/>
<input style="float:left; width:100%; height:20px; margin-bottom: 20px;" name="contactemail" id="contactemail" type="text" placeholder="contactemail"/>
<input style="float:left; width:100%; height:20px; margin-bottom: 20px;" name="contactphone" id="contactphone" type="text" placeholder="contactphone"/>
<input style="float:left; width:100%; height:20px;" name="description" id="description" type="text" placeholder="description"/>
<input style="float:left; width:100%; height:20px;" name="notes" id="notes" type="text" placeholder="notes"/>
<button class="addnewproject" id="<?php echo $row["accid"]; ?>" >Add</button>
<button onclick="closenewprojectwindow()">Cancel</button>
</td>
</tr>
</table>
</form>
My SQL:
<?php
include 'config.php';
$sqlupdateincome = "INSERT INTO projects (username, projectname)
VALUES ('". $_POST['username'] ."', 'Ochrom Test Project')";
if ($conn->query($sqlupdateincome) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Here is my database just in case it might be that. click for image
I do appreciate all help and responses, thank you!
Lets break this down. You have a form tag, which means you want to submit a form. Without an action attribute, you will be at the will of the browser, but pretty much all will submit the form the same page.
<form id="addnewprojecttable" method="post">
Further down, still within the form, you have a button. The default action of a button is to submit the form unless you have modified the function of the button with Javascript, which is what you kind of have.
<button class="addnewproject" id="<?php echo $row["accid"]; ?>" >Add</button>
I do NOT use JQuery at all, but I can read it enough to understand what you are doing. Your modification code for the button is saying. Do NOT submit the form with this line:
event.preventDefault();
But then continue with the rest of the code in the function.
Without this line, the rest of the function will still be actioned and the form will be submit to itself as well (default action of the HTML). It may also be possible that the browser does not finish actioning the Javascript function before the form submission (inconsistent results - not a good thing).
Depending on your layout, sometimes you may need the following, but that is really another discussion.
event.stopPropagation();
event.preventDefault();
It is good practice to allow your HTML to work without Javascript enabled as well. For this reason, including an action in your form would also be something I would recommend.
Take note: Your SQL is open to SQL Injection as well.
Edit:
What is the full path to config.php and components/sql/insertproject.php?
On insertproject.php, add the following to the top:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
And then happens when you go directly to the URL /components/sql/insertproject.php? Is there an error displayed?

CSS Declaration for Input sequential <input id =" sequential list" > Generated by PHP

I have an input declaration (within a bootstrap modal) as follows:
<input type="range" id="seek-bar_<?php echo $i+1;?>" value="0" >
where i could be a large number. I need the width of this bar to be 30px, so I added a class to the above to allow the width to be defined within the style class, i.e.
<input type="range" id="seek-bar_<?php echo $i+1;?>" value="0" class ="seek-bars" >
and then declared in my css file,
.seek-bars {
width: 30px;
}
And no matter what this does not work, but declaring
#seek-bar_1 {
width: 30px;
}
does work? So, I don't think there is anything wrong with my class definitions and declarations so it this will not work is there a way of declaring a wildcard id match? For example #seek-bar_*.
I could just put the style declaration within the tag but this will cause other problems for me and would like to have it resolved within my CSS file if possible.
Any help would be much appreciated.
Thanks
You can use CSS [attribute^=value] selector. This selector matches every element whose attribute value begins with a specified value. In our case its seek-bar_. Like:
input[id^='seek-bar_'] {
width: 30px;
border: 1px solid red;
}
Have a look at the example snippet below:
input[id^='seek-bar_'] {
width: 30px;
border: 1px solid red;
}
<input type="text" id="seek-bar_one" />
<input type="text" id="seek-bar_two" />
<input type="text" id="seek-bar_three" />
<input type="text" id="seek-bar_four" />
Hope this helps!

Can't use NAME in form can I use ID instead?

I am working on a project that is developed using Kohana and due to some conditions I have no access to use NAME in the forms instead I have an option to use ID but I tried using the following method which didn't work.
<form action="sendmail.php" method="post">
<p><input type="text" size="30" style="border-radius:15px; border:2px solid #000; padding:5px;" placeholder="Name" id="contname" /><br />
<br />
<input type="text" size="30" style="border-radius:15px; border:2px solid #000; padding:5px;" placeholder="Email" id="contemail" /><br />
<br />
<input type="text" size="30" style="border-radius:15px; border:2px solid #000; padding:5px;" placeholder="Subject" id="contsubject" /><br />
<br />
<textarea style="border-radius:5px; border:2px solid #000; padding:5px; width:320px; height:120px;" id="contmessage" placeholder="Message"></textarea><br />
<br />
<input type="submit" value="SUBMIT" style="background-color:#9377dd; border-radius:10px; padding-top:3px; padding-bottom:3px; padding-left:16px; padding-right:16px;" /></p>
</form>
sendmail.php
<?php
$from = $_POST["contemail"];
$message = $_POST["contname"] . "<br/>". $_POST["contsubject"] . "<br/>" . $_POST["contmessage"];
mail("me#mail.com","From contact form",$message,"From: $from\n");
mail("me#mail.com","From contact form",$message,"From: $from\n");
header('Location: faq');
?>
Any alternate method please???
When you post the form then in server side you can access that field by their name. Now you are not using the name and instead of you are using the id then I will suggest you to use javascript ajax method to post the form. This is the only alternative method is available.
When you post a form, the form is converted into key/value pairs and sent to the server. For example...
<input name="MyName" value="MyValue">
Is sent to the server as
MyName=MyValue
Unless the browser can form a key/value pair it won't send the data - this is true whether you are missing the key (from the name attribute) or a value (for example a checkbox that is not checked).
You could iterate over the form using JavaScript and create a form post using a different attribute (such as your ID), but if you have access to add JavaScript to this form, it would be easier to just add names.

My PHP credit card form not recognizing certain inputs and errors

I was following a tutorial from John Conde on Authorize.net's credit card input form using PHP and error dectection.
It went perfect but I decided to add input boxes for entering the payment amount and and removed the unneeded shipping address requirements;
Now when the submitted form inputs are incorrect or empty, they no longer turn red nor does the "amount" box actually recognize whether it's empty or filled. The error box still pops up to bad credit card submissions.
Here's the page(minus the design to simplify trouble shooting);
http://teetimelawncare.com/payment-form.php
EDIT: removed the non-credit card related code and stuff like the state and year expiration dates to make it smaller. the PHP code at the very bottom is for the red error popup box that shows to the user when they incorrectly fill out the form.
I was at this part of the tutorial if anyone wants to compare:
http://community.developer.authorize.net/t5/The-Authorize-Net-Developer-Blog/Handling-Online-Payments-Part-5-Processing-Payment-and-Handling/ba-p/10768
Code:
<?php
$errors = array();
if ('POST' === $_SERVER['REQUEST_METHOD'])
{
$credit_card = sanitize($_POST['credit_card']);
$expiration_month = (int) sanitize($_POST['expiration_month']);
$expiration_year = (int) sanitize($_POST['expiration_year']);
$cvv = sanitize($_POST['cvv']);
$cardholder_first_name = sanitize($_POST['cardholder_first_name']);
$cardholder_last_name = sanitize($_POST['cardholder_last_name']);
$billing_address = sanitize($_POST['billing_address']);
$billing_address2 = sanitize($_POST['billing_address2']);
$billing_city = sanitize($_POST['billing_city']);
$billing_state = sanitize($_POST['billing_state']);
$billing_zip = sanitize($_POST['billing_zip']);
$telephone = sanitize($_POST['telephone']);
$email = sanitize($_POST['email']);
$account = sanitize($_POST['account']);
$amount = sanitize($_POST['amount']);
if (!validateCreditcard_number($credit_card))
{
$errors['credit_card'] = "Please enter a valid credit card number";
}
if (!validateCreditCardExpirationDate($expiration_month, $expiration_year))
{
$errors['expiration_month'] = "Please enter a valid exopiration date for your credit card";
}
if (!validateCVV($credit_card, $cvv))
{
$errors['cvv'] = "Please enter the security code (CVV number) for your credit card";
}
if (empty($cardholder_first_name))
{
$errors['cardholder_first_name'] = "Please provide the card holder's first name";
}
if (empty($cardholder_last_name))
{
$errors['cardholder_last_name'] = "Please provide the card holder's last name";
}
if (empty($billing_address))
{
$errors['billing_address'] = 'Please provide your billing address.';
}
if (empty($billing_city))
{
$errors['billing_city'] = 'Please provide the city of your billing address.';
}
if (empty($billing_state))
{
$errors['billing_state'] = 'Please provide the state for your billing address.';
}
if (!preg_match("/^\d{5}$/", $billing_zip))
{
$errors['billing_zip'] = 'Make sure your billing zip code is 5 digits.';
}
if (empty($telephone))
{
$errors['telephone'] = 'Please provide a telephone number where we can reach you if necessary.';
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$errors['email'] = 'Please provide a valid email address';
}
if (empty($account))
{
$errors['account'] = 'Please provide the Your Customer ID Number from your billing statement.';
}
if (empty($amount))
{
$errors['amount'] = 'Please enter a payment amount.';
}
// If there are no errors let's process the payment
if (count($errors) === 0)
{
// Format the expiration date
$expiration_date = sprintf("%04d-%02d", $expiration_year, $expiration_month);
// Include the SDK
require_once('./config.php');
// Process the transaction using the AIM API
$transaction = new AuthorizeNetAIM;
$transaction->setSandbox(AUTHORIZENET_SANDBOX);
$transaction->setFields(
array(
'amount' => $amount,
'card_num' => $credit_card,
'exp_date' => $expiration_date,
'first_name' => $cardholder_first_name,
'last_name' => $cardholder_last_name,
'address' => $billing_address,
'city' => $billing_city,
'state' => $billing_state,
'zip' => $billing_zip,
'email' => $email,
'card_code' => $cvv,
'Customer ID Number' => $account,
)
);
$response = $transaction->authorizeAndCapture();
if ($response->approved)
{
// Transaction approved. Collect pertinent transaction information for saving in the database.
$transaction_id = $response->transaction_id;
$authorization_code = $response->authorization_code;
$avs_response = $response->avs_response;
$cavv_response = $response->cavv_response;
// Put everything in a database for later review and order processing
// How you do this depends on how your application is designed
// and your business needs.
// Once we're finished let's redirect the user to a receipt page
header('Location: thank-you-page.php');
exit;
}
else if ($response->declined)
{
// Transaction declined. Set our error message.
$errors['declined'] = 'Your credit card was declined by your bank. Please try another form of payment.';
}
else
{
// And error has occurred. Set our error message.
$errors['error'] = 'We encountered an error while processing your payment. Your credit card was not charged. Please try again or contact customer service to place your order.';
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Payment Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Language" content="en-us">
<style type="text/css">
#errormessage
{
background-color: #FFE7E7;
border: 3px solid #CC0033;
color: #000000;
margin: 20px ;
padding: 10px;
width: 420px;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
-moz-box-shadow: 5px 5px 5px #ccc;
-webkit-box-shadow: 5px 5px 5px #ccc;
box-shadow: 5px 5px 5px #ccc;
background: -webkit-gradient(linear, 0 0, 0 bottom, from(#FFEAEA), to(#FFB3B3));
background: -moz-linear-gradient(#FFEAEA, #FFB3B3);
background: linear-gradient(#FFEAEA, #FFB3B3);
float: left;
}
.labelerror
{
color: #ff0000;
font-weight: bold;
}
h3 {
font-size: 1.6em;
line-height: 10px;
padding-left: 17px;
padding-top: 8px;
-webkit-font-smoothing: antialiased;;
}
#credit
{
Position: relative;
margin-left: 14px;
height:620px;
width:400px;
-webkit-border-radius: 6px;
border-radius: 6px;
-moz-box-shadow: 5px 5px 5px #ccc;
-webkit-box-shadow: 5px 5px 5px #ccc;
box-shadow: 5px 5px 5px #ccc;
float: left;
}
#amount1
{
margin: 5px;
height:620px;
position: relative;
width:400px;
-webkit-border-radius: 6px;
border-radius: 6px;
-moz-box-shadow: 5px 5px 5px #ccc;
-webkit-box-shadow: 5px 5px 5px #ccc;
box-shadow: 5px 5px 5px #ccc;
float: left;
}
</style>
</head>
<body>
<div id="amount1"> <h3> Payment Amount</h3><p>
<form id="myform"> <label for="amount"<?php if (in_array('amount', $errors)) echo ' class="labelerror"'; ?>> $</label>
<input type="text" name="amount" id="amount" maxlength="5" value=""></form>
</p> <br><div id="phpdisplay"> <form action="payment-form.php" method="get" enctype="application/x-www-form-urlencoded" target="_self" id="search">
<strong>Get your current balance by searching<br> your Customer ID number</strong><br>(Don't Know? Ask us on live chat or check your billing invoice):<br> <input type="text" name="term" /><br />
<input type="submit" name="btn" value="Search" />
</form>
</form></div>
<div id="credit">
<h3> Credit Card Information</h3>
<form id="myform" action="/payment-form.php" method="post">
<p>
<label for="credit_card"<?php if (in_array('credit_card', $errors)) echo ' class="labelerror"'; ?>>Credit Card Number</label>
<input type="text" name="credit_card" id="credit_card" autocomplete="off" maxlength="19" value="">
</p>
<p>
<label for="expiration_month"<?php if (in_array('expiration_month', $errors)) echo ' class="labelerror"'; ?>>Expiration Date</label>
<select name="expiration_month" id="expiration_month">
<option value="12">12</option>
</select>
<select name="expiration_year" id="expiration_year">
<option value="0"> </option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
</select>
</p>
<p>
<label for="cvv"<?php if (in_array('cvv', $errors)) echo ' class="labelerror"'; ?>>Security Code</label>
<input type="text" name="cvv" id="cvv" autocomplete="off" value="" maxlength="4">
</p>
<p>
<label for="cardholder_first_name"<?php if (in_array('cardholder_first_name', $errors)) echo ' class="labelerror"'; ?>>Cardholder's First Name</label>
<input type="text" name="cardholder_first_name" id="cardholder_first_name" maxlength="30" value="">
</p>
<p>
<label for="cardholder_last_name"<?php if (in_array('cardholder_last_name', $errors)) echo ' class="labelerror"'; ?>>Cardholder's Last Name</label>
<input type="text" name="cardholder_last_name" id="cardholder_last_name" maxlength="30" value="">
</p>
<p>
<label for="billing_address"<?php if (in_array('billing_address', $errors)) echo ' class="labelerror"'; ?>>Billing Address</label>
<input type="text" name="billing_address" id="billing_address" maxlength="45" value="">
</p>
<p>
<label for="billing_address2"<?php if (in_array('billing_address2', $errors)) echo ' class="labelerror"'; ?>>Suite/Apt #</label>
<input type="text" name="billing_address2" id="billing_address2" maxlength="45" value="">
</p>
<p>
<label for="billing_city"<?php if (in_array('billing_city', $errors)) echo ' class="labelerror"'; ?>>City</label>
<input type="text" name="billing_city" id="billing_city" maxlength="25" value="">
</p>
<p>
<label for="billing_state"<?php if (in_array('billing_state', $errors)) echo ' class="labelerror"'; ?>>State</label>
<select id="billing_state" name="billing_state">
<option value="0"> </option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
</select>
</p>
<p>
<label for="billing_zip"<?php if (in_array('billing_zip', $errors)) echo ' class="labelerror"'; ?>>Zip Code</label>
<input type="text" name="billing_zip" id="billing_zip" maxlength="5" value="">
</p>
<p>
<label for="telephone"<?php if (in_array('telephone', $errors)) echo ' class="labelerror"'; ?>>Telephone Number</label>
<input type="text" name="telephone" id="telephone" maxlength="20" value="">
</p>
<p>
<label for="email"<?php if (in_array('email', $errors)) echo ' class="labelerror"'; ?>>Email Address</label>
<input type="text" name="email" id="email" maxlength="20" value="">
</p>
<p>
<label for="account"<?php if (in_array('account', $errors)) echo ' class="labelerror"'; ?>>Customer ID number</label>
<input type="text" name="account" id="account" maxlength="6" value="">
</p>
<p>
<input type="submit" value="Checkout">
</p>
</form></div><?php
if (count($errors))
{
?>
<div id="errormessage">
<h2>
There was an error with your submission. Please make the necessary corrections and try again.
</h2>
<ul>
<?php
foreach ($errors as $error)
{
?>
<li><?php echo $error; ?></li>
<?php
}
?>
</ul>
</div>
<?php
}
?>
</body>
</html>
Lastly, I wanted to move the checkout button outside the div form so I made the button like this(in the designed page, not the example above)
</form> <br>
<form id="myform"><p class="center">
<button form="myform" input type="submit" value="Checkout">
</p></form>
The button works but it's not displaying the value as the label on my (WIP) designed page.
This:
<button form="myform" input type="submit" value="Checkout">
is not how the <button> element is constructed. It looks like you attempted to change an <input />. This is likely what you're after:
<button form="myform" type="submit">Checkout</button>
It also looks like you're duplicating the id on two different forms, which is invalid. Remove the id on the form that wraps the submit button, or change it to something else.
This is actually several questions it seems to me. Since there's several, I might get something mixed up, someone point out if I get something blaringly wrong.
RE: ""amount" box actually recognize whether it's empty or filled." --
You can't split the amount off into it's own form and have it go along with the rest of the elements in the other form element. Everything you want to post has to be in the same form element. (Unless you use the html5 form attribute, but I don't think IE supports this yet. Someone correct me if I'm wrong please. Even then, you wouldn't be adding more form elements if I recall correctly.) See: Is it possible to wrap html form elements in multiple form tags? See the comments in the accepted answer for more details.
Regarding the boxes not changing with errors. --
<label for="billing_address2"<?php if (in_array('billing_address2', $errors)) echo ' class="labelerror"'; ?>>Suite/Apt #</label>
Should probably be:
<label for="billing_address2"<?php if (in_array('billing_address2', array_keys($errors))) echo ' class="labelerror"'; ?>>Suite/Apt #</label>
Your array is keyed with the element names, so your in_array should search the keys of the errors array. (Note that this will change the labels colors, not the input boxes themselves. Put the class-setting code on the boxes if you want the boxes themselves to change.)
Button is addressed in another answer:
<button form="myform" type="submit">Checkout</button>
HTML5 outside of form element. Again, not sure if IE supports this. No need to wrap it in a form element btw, assuming you're targeting browsers that support the form attribute.
<button type="submit">Checkout</button>
Inside form.

JQuery/Javascript select-all by clicking link or selecting "select-all" from a checkbox dropdown

I have a private messaging system and would like to have about 4-5 links above the inbox where a user can click if they want to "select all", "none", "favourite", "read" or "unread" messages.
How would I do this using jquery/javascript? Are there any tutorials about that explain this thoroughly? I am not that great with javascript but I'm a quick learner.
I originally really wanted to do a gmail style checkbox drop down but it is proving to be quite difficult and I think having links across of the message inbox would be more user friendly..
I hacked together a simple example of how I would do this, you will have to tailor it to your needs, but it should get you started.
Just try this (make sure you change it to match your jquery library):
<html>
<body>
<div class="mesg" id="mesg1" read="1" favorite="0">
<input type="checkbox" name="check1">
message 1 info
</div>
<div class="mesg" id="mesg2" read="1" favorite="1">
<input type="checkbox" name="check2">
message 2 info
</div>
<div class="mesg" id="mesg3" read="0" favorite="0">
<input type="checkbox" name="check3">
message 3 info
</div>
<input type="button" value="select read" id="select_read" />
<input type="button" value="select favorite" id="select_fav" />
<script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function() {
$("#select_read").click(function() {
$("div.mesg input[type=checkbox]").attr("checked", false);
$("div.mesg[read=1] input[type=checkbox]").attr("checked", true);
});
$("#select_fav").click(function() {
$("div.mesg input[type=checkbox]").attr("checked", false);
$("div.mesg[favorite=1] input[type=checkbox]").attr("checked", true);
});
});
</script>
</body>
</html>
My solution on jsFiddle: http://www.jsfiddle.net/pereskokov/sYe4S/6/
HTML:
<p id="links">
Select all,
none,
unread,
read,
favourite
</p>
<p id="messages">
<label class="unread">
<input type="checkbox" name="message" value="1" class="unread" /> Hi, man!
</label><br/>
<label class="read fav">
<input type="checkbox" name="message" value="2" class="read fav" /> Cute kittens, look
</label><br/>
<label class="read">
<input type="checkbox" name="message" value="3" class="read" /> Pysh-pysh, ololo
</label><br />
<label class="unread">
<input type="checkbox" name="message" value="4" class="unread" /> New important task!
</label><br/>
</p>
CSS:
label.unread {
font-weight: bold;
}
label.fav {
background-color: #F5E942;
}
a.pseudo {
text-decoration: none;
border-bottom: 1px dashed #4998C9;
color: #4998C9;
}
a.active {
background-color: #4998C9;
color: white;
padding: 0 0.2em;
}
jQuery:
$('#links').delegate('a', 'click', function(ev) {
// reset all checkboxes
$('input:checkbox').attr('checked', false);
// get info, what is the user choice
whichMessages = $(this).attr('id');
// do our main work - select checkboxes
switch (whichMessages) {
case 'all':
$('input:checkbox').attr('checked', true);
break;
case 'read':
$('input:checkbox.read').attr('checked', true);
break;
case 'unread':
$('input:checkbox.unread').attr('checked', true);
break;
case 'fav':
$('input:checkbox.fav').attr('checked', true);
break;
};
// add some user-frendly markup
$('#links a').removeClass('active');
$(this).addClass('active');
// and standart action to prevent standart link click event
ev.preventDefault();
});
Excuse me, I am the man, who gave you fish, but did not teach you to catch fish.
Use jquery, then
$(".parent").find("input[type=checkbox]").each(function() {
$(this).checked = true;
});
Obviously this is just an example and you won't be able to simply copy paste this, but this should get you started.

Categories