Clicking on form label focuses the wrong input box - php

I have a form that contains two input boxes for first and last name.
When I click the "Last Name" label, the cursor focuses on the "First Name" field (instead of the "Last Name" field as expected). How do I fix this problem?
$(document).on('click', 'a', function() {
$('div.tabClass').addClass('hide');
$($(this).attr('href')).removeClass('hide');
} );
.hide {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form action='' method='POST'>
<ul>
<li>
<a href='#first'>
First Tab
</a>
</li>
<li>
<a href='#secound'>
Second Tab
</a>
</li>
<li>
<a href='#third'>
third Tab
</a>
</li>
<li>
<a href='#fourth'>
Fourth Tab
</a>
</li>
</ul>
<div id='first' class='hide tabClass'>
<label>
First Name :
<input type='text' name='name' id='name'>
<br/>
<label>
Last Name :
<input type="text" name='sname' id="sname">
<input type="submit" value="Click">
</div>
</form>

You need to associate the labels with the input fields.
<label for="name">
First Name :
<input type="text" name="name" id="name">
</label>
<label for="sname">
Last Name :
<input type="text" name="sname" id="sname">
</label>
As #Sumurai8 mentioned in the comments, the <label> element requires a closing tag, which is missing from your code.

Related

how to post comment below the text box after clicking submit button?

am trying to make a "Add your recipe" page in my website.. i have tried this form and it shows perfectly.. but how do i post the recipe below the form after clicking submit button according to the recipe format?
<center>
<form method="post">
<div class='wrapperr'>
<div class='wrapper'>
<div class='container'>
<div class='top'>
<div class='logo'></div>
<div class='gray close'></div>
</div>
<div class='content'>
<ul class='section'>
<li class='header'></li>
<li class='item right-label'>
<label for="title">Title</label>
<input type="text" placeholder='eg. Chocolate Chip Cookies' id="title" class='shorter' />
<div class='picture-container'>
<div class='picture'></div>
</div>
</li>
<li class='item right-label'>
<label for="author">Author</label>
<input type="text" placeholder='eg. Julia Child (optional)' id="author" class='shorter' />
</li>
<li class='item right-label'>
<label for="adapted">Adapted</label>
<input type="text" placeholder='eg. The Joy of Cooking (optional)' id="adapted" class='shorter' />
</li>
<li class='item right-label'>
<label for="adapted-link">Adapt Link</label>
<input type="text" placeholder=' (optional)' id="adapted-link" class='shorter' />
<input type="button" class="button green box-shadow" value="Select Image" id="picture" />
</li>
<li class='item spaced right-label'>
<label for="summary" id="summary-label">Description</label>
<textarea placeholder='eg. A delicious drop cookie (optional)' id="summary"></textarea>
</li>
<li class='item right-label'>
<label for="prep-time">Prep Time</label><input type="text" placeholder="HH:MM (optional)" id="prep-time" class="time" />
<label for="cook-time">Cook Time</label><input type="text" placeholder="HH:MM (optional)" id="cook-time" class="time" />
<label for="total-time">Total Time</label><input type="text" placeholder="HH:MM (optional)" id="total-time" class="time" />
</li>
<li class='item right-label'>
<label for="servings">Serves</label><input type="text" placeholder="Amount (optional)" id="servings" class="amount" />
<label for="yields">Yields</label><input type="text" placeholder="Amount (optional)" id="yields" class="amount" />
</li>
<li class='header new'></li>
<li class='item'>
<label for="ingredients">Ingredients</label>
<textarea placeholder="One ingredient per line, include subheaders by ending with a colon (eg. For the cake:)" id="ingredients" class='larger'></textarea>
</li>
<li class='item'>
<label for="directions">Directions</label>
<textarea placeholder="One direction per line, include subheaders by ending with a colon (eg. For the cake:)" id="directions" class='larger'></textarea>
</li>
<li class='item'>
<label for="notes">Notes</label>
<textarea placeholder="One note per line, include subheaders by ending with a colon (eg. Great tip:) (optional)" id="notes" class='larger'></textarea>
</li>
<li class='header new error-list' style="display: none"></li>
<li class='new error-data' style="display: none">
<ul class='error-ul'>
<li class='error-title' style="display: none">Please enter a title for the recipe.</li>
<li class='error-adapted-link' style="display: none">Please enter a valid url.</li>
<li class='error-prep-time' style="display: none">Please enter a prep time using HH:MM such as <b>1:15</b> for an hour and fifteen minutes or <b>25</b> for 25 minutes.</li>
<li class='error-cook-time' style="display: none">Please enter a cook time using HH:MM such as <b>1:15</b> for an hour and fifteen minutes or <b>25</b> for 25 minutes.</li>
<li class='error-total-time' style="display: none">Please enter a total time using HH:MM such as <b>1:15</b> for an hour and fifteen minutes or <b>25</b> for 25 minutes.</li>
<li class='error-servings' style="display: none">Please enter a number for the serves amount such as <b>4</b> for a recipe that makes 4 servings.</li>
<li class='error-yields' style="display: none">Please enter a number for the yields amount such as <b>18</b> for a recipe that makes 18 cookies.</li>
<li class='error-ingredients' style="display: none">Please enter some ingredients for your recipe.</li>
</ul>
</li>
<li class='new center'>
<input type="button" class='button green standard-button box-shadow' value='Save' id="save" />
<input type="button" class='button white standard-button box-shadow' value='Cancel' id="cancel" />
<div class='working'>
<div class='working-image'></div>
<div class='working-text'>Saving Recipe</div>
</div>
</li>
</ul>
</div>
</div>
</div></center>`
Here is the link to the comments box made using HTML and JavaScrit. It doesn't work on the submit button but serves the purpose.
HTML:
<form>
<textarea id="comment" placeholder="Say something..." /></textarea><br>
Post comment!
</form>
<div id="comments">
</div>
JavaScript:
window.postComment=function()
{
var div = document.getElementById("comments");
div.innerHTML = div.innerHTML +"<br>"+ document.getElementById("comment").value;
}
Fiddle here.
Create a php file called 'Config.php'
class Config {
public static function get($item){
if(isset($_POST[$item])){
return $_POST[$item];
} else if(isset($_GET[$item])) {
return $_GET[$item];
}
return '';
}
}
then add a require_once 'Config.php' to your page with the form
if(isset($_POST['submit']){ // if user submits form
echo Config::get('comments');
}
The function will check the form method, and return the $_POST or $_GET value. We then check if the user has submitted the form. Once they do, it will get the value and echo it to the page.

two javascript forms on one page

I cant get the two forms to play with each other.. - meaning I have one form that sends an email - and another that is using a search function. The email form is working ok. Validates just fine, but when I click on the search button, it doesn't do anything. ( the search form is working fine if I remove the email form )
What am I missing ??.. ugh..
I've been working on this, but my brain is fried. I am sure it is something simple.
Any help from you guys would be much appreciated.
<form method="post" action="contact.php" name="contactform" id="contactform">
<div class="full-lenght">
<div class="to-left">Name<br>
<span class="small-required">Required</span> </div>
<div class="to-right">
<input name="name" type="text" id="name" value="" class="contact-field" />
</div>
</div>
<div class="clear"></div>
<div class="full-lenght">
<div class="to-left">Email<br>
<span class="small-required">Required</span> </div>
<div class="to-right">
<input name="email" type="text" id="email" value="" class="contact-field" />
</div>
</div>
<div class="clear"></div>
<div class="full-lenght">
<div class="to-left">Phone<br>
<span class="small-required">Required</span> </div>
<div class="to-right">
<input name="phone" type="text" id="phone" value="" class="contact-field" />
</div>
</div>
<div class="clear"></div>
<div class="full-lenght">
<div class="to-left">Message<br>
<span class="small-required">Required</span> </div>
<div class="to-right">
<textarea name="comments" rows="3" id="comments" class="contact-message"></textarea>
</div>
</div>
<div class="clear"></div>
<div class="clear"></div>
<div class="human">3 + 1 = ?<br>
<input name="verify" type="text" id="verify" size="4" value="" style="width: 30px;" />
<input name="e" type="submit" class="contact-button" id="submit" value="Submit" />
</div>
</form>
<div class="search">
<form class="form" action="search_results.php" method="get">
<input name="q" type="text" class="search_field" value="Search.." onfocus="if (this.value=='Search..') this.value='';">
<a onclick=" document.forms[0].submit();return false" href="#"><img class="search_button" title="Search Button" src="images/search_button.jpg" alt="" width="24" height="24"></a> <br>
<br>
</form>
</div>
This is the code. The second form starts in search div. The first one is an email form with ajax validation.
try this,
in the second form
<form class="form" id="searchform" action="search_results.php" method="get">
<input name="q" type="text" class="search_field" value="Search.." onfocus="if (this.value=='Search..') this.value='';" />
<a onclick=" document.getElementById('searchform').submit();return false" href="#"><img class="search_button" title="Search Button" src="images/search_button.jpg" alt="" width="24" height="24"/></a> <br/>
<br/>
</form>
This doesn't have anything to do with PHP, but does have everything to do with JavaScript.
Change:
<a onclick=" document.forms[0].submit();return false" href="#">
to:
<a onclick="document.forms[1].submit();" href="#">
You're using legacy DOM notation in your JavaScript, which is OK but outdated. When you have forms[0] you're referring to the first form in the DOM which isn't the search form (the contact form is), forms[1] is the search form. Clicking the icon will fail without the change, but you could type in the box and hit enter and it will work, since the JavaScript you have won't be triggered (no click). I also see no need to keep the return false statement in there.
<form class="form" action="search_results.php" method="get">
<input name="q" type="text" class="search_field" value="Search.." onfocus="if (this.value=='Search..') this.value='';">
<input type="submit" value='Search'> <br>
<br>
This problem you used anchor tag as button form and it doesn't work for submitting form so you can either replace anchor tag with input tag "submit type" as you see in code or you can use JavaScript to make anchor tag when you click on it, it fires a submitting form.
Enjoy :)

How to call a jQuery function on click event in the following scenario?

I'm using PHP, Smarty and jQuery for my website. There is a functionality of showing and hiding an element on a form. The HTML as well as code from jQuery function is as below:
HTML Code:
<div class="c-mega-accord">
<ol class="fnAccordion">
<li>
<a class="fnTrigger" href="#">Practice Sheet Basic Details <span></span></a>
<div class="fnAccContent">
<div class="c-grad-box">
<div class="form-wrapper">
{if $error_msg}<div class="error-info">{$error_msg.error_msgs}</div>{/if}
<form name="manage_practice_sheet" id="manage_practice_sheet" action="practice_sheet.php" method="post">
<input type="hidden" name="op" id="op" value="{$op}" />
<input type="hidden" name="sheet_type" id="sheet_type" value="{$sheet_type}" />
<input type="hidden" name="form_submitted" id="form_submitted" value="yes" />
<input type="hidden" name="practice_sheet_id" id="practice_sheet_id" value="{$practice_sheet_id}" />
<input type="hidden" name="hidden_practice_sheet_category_id" id="hidden_practice_sheet_category_id" value="{$practice_sheet_category_id}" />
<input type="hidden" name="practice_sheet_id" id="practice_sheet_id" value="{$practice_sheet_id}" />
<p class="form-info fl-right">* Mandatory fields</p>
<ul>
<li>
<label>{'Category'|signal_on_error:$error_msg:'practice_sheet_category_id'}<span class="reqd">*</span></label>
<div class="form-element">
<select name="practice_sheet_category_id" id="practice_sheet_category_id" <!--onChange="get_subcategory_by_category('{$control_url}modules/category/manage_category.php', this.value, 'get_subcategory_by_category', '#practice_sheet_sub_category_id');"--> >
<option value="">Select</option>
{if $all_parent_categories}
{foreach from=$all_parent_categories item="parent_category"}
<option value="{$parent_category.category_id}" {if $data.practice_sheet_category_id==$parent_category.category_id || $practice_sheet_category_id==$parent_category.category_id} selected="selected"{/if}>{$parent_category.category_name}</option>
{/foreach}
{/if}
</select>
</div>
</li>
<li>
<label>{'Practice Sheet Name'|signal_on_error:$error_msg:'practice_sheet_name'}<span class="reqd">*</span></label>
<div class="form-element">
<input class="" type="text" name="practice_sheet_name" id="practice_sheet_name" value="{$data.practice_sheet_name}" maxlength="50">
</div>
</li>
<li>
<label>Display Date</label>
<div class="form-element">
<input type="text" class="cal fl-left" id="frmDate" name="frmDate" value="{if $data.practice_sheet_display_date !='0' && $data.practice_sheet_display_date !=''}{$data.practice_sheet_display_date}{/if}">
</div>
</li>
<li>
<label>Practice Sheet For</label>
<div class="form-element">
<input class="" type="text" name="practice_sheet_for" id="practice_sheet_for" value="{$data.practice_sheet_for}" maxlength="50">
</div>
</li>
<li>
<label></label>
<div class="form-element">
<!--<input type="submit" value="{$submit_value}" class="c-btn" id="saveAddMore" name="submit">
<input type="button" value="{$cancel_value}" class="c-gray-btn" id="done" name="done" onclick="javascript:window.location.href='{$control_url}modules/practice_sheet/practice_sheet.php?op={$query_string}'"><br/>-->
<span class="c-btn c-continus-btn"><input type="button" name="continus" id="continus" value="Continue" id="" name=""></span>
<span class="c-gray-btn c-cancel-btn"><input type="button" value="Cancel" id="" name=""></span>
</div>
</li>
</ul>
</form>
</div>
</div>
</div>
</li>
<li>
<a class="fnTrigger" href="#">Select Category <span></span></a>
<div class="fnAccContent">
<div class="c-grad-box">
</div>
</div>
</li>
</ol>
</div>
jQUery Code:
function accordion(){
var li = $(".fnAccordion > li");
li.eq(0).addClass("active");
li.children('.fnAccContent').not(':first').hide();
$(".fnAccordion .fnTrigger").click(function(e){
e.preventDefault();
var numLi = $(this).parent('li').siblings();
if(numLi.length > 0){
$(this).parent('li').siblings().removeClass("active");
var curState = $(this).parent().find(".fnAccContent").css("display");
if(curState == "none"){
$(".fnAccContent").slideUp();
$(this).parent().addClass("active");
$(this).parent().find(".fnAccContent").slideDown();
}
}else{
$(this).parent('li').toggleClass("active");
$(this).parent().find(".fnAccContent").slideToggle();
}
})
}
$(document).ready(function(){
accordion();
})
Now the functionality of hide/show is working fine when I click on following two elements:
<a class="fnTrigger" href="#">Practice Sheet Basic Details <span></span></a>
<a class="fnTrigger" href="#">Select Category <span></span></a>
Actually I want to make this functionality work on Continue button(following is the HTML code snippet for it):
<input type="button" name="continus" id="continus" value="Continue" id="" name="">
I tried to make it work on Continue button by applying the class fnTrigger but it didn't work. Can you help me in this regard? Thanks in advance.
If you want the function to be triggered when both the classes are present,
you use
$(".fnAccordion .fnTrigger").click(function(){
//Your code here
});
If you want the function to get triggered when element of either class is clicked, use a "," to separate the classes.
$(".fnAccordion, .fnTrigger").click(function(){
//Your code here
});
Your check only checks for the fnTrigger class INSIDE an element with the class fnAccordion
$(".fnAccordion .fnTrigger").click(function(e){});
Is your button inside the element with the class fnAccordion?

How to find out a given text is present or not with in a large data in javascript

In my php project i have a javascript variable say "addText" which contains the following html content
<div class="foxcontainer" style="width:100% !important;">
<h2>FREE IN HOME CONSULTATION</h2>
<ul class="fox_messages">
<li>Invalid field: Your name</li>
<li>Invalid field: Your email</li>
</ul>
<form enctype="multipart/form-data" class="foxform" action="/hima/bigriglawgroup/index.php/about-us#mid_131" method="post">
<!-- mod_foxcontact 2.0.19 GNU/GPLv3 -->
<div class="foxfield">
<input class="invalidfoxtext" value="Your name" title="Your name" style="width:85% !important;display:block;float:none;margin:0 auto !important;" name="_c31f7a92d344f9a5c23d07fd438ba0b6" onfocus="if(this.value==this.title) this.value='';" onblur="if(this.value=='') this.value=this.title;" type="text">
<span class="asterisk"></span>
</div>
<div class="foxfield">
<input class="invalidfoxtext" value="Your email" title="Your email" style="width:85% !important;display:block;float:none;margin:0 auto !important;" name="_4fd8a7bb907c63baca708086b63eb7f0" onfocus="if(this.value==this.title) this.value='';" onblur="if(this.value=='') this.value=this.title;" type="text">
<span class="asterisk"></span>
</div>
<div class="foxfield">
<input class="validfoxtext" value="Phone number" title="Phone number" style="width:85% !important;display:block;float:none;margin:0 auto !important;" name="_68a05eb930e9ce854ed0c2d2d25c14a1" onfocus="if(this.value==this.title) this.value='';" onblur="if(this.value=='') this.value=this.title;" type="text">
</div>
<div class="foxfield">
<textarea rows="" cols="" class="validfoxtext" name="_0ef408e84316ef5737db72a727579f48" title="Notes" style="width:85% !important;height:180px !important;display:block;float:none;margin:0 auto !important;" onfocus="if(this.value==this.title) this.value='';" onblur="if(this.value=='') this.value=this.title;">retret ret ret ret</textarea>
</div>
<div class="foxfield">
<button class="foxbutton" type="submit" style="margin-right:32px;" name="mid_131">
<span>Submit</span>
</button>
</div>
<div class="fox-copyright" style="padding:10px 0 !important;text-indent:0 !important">
<a target="_blank" title="Joomla contact form" href="#" style="display: none !important; display: inline !important; font-size:10px !important;">powered by fox contact</a>
</div>
</form>
</div>
here I want to check the string "Invalid field" is present or not in that content. If it's not present I want to do some other functionality.But I don't know How to do this
Does any one know this ? Please help me ASPS.
Thanks in advance
if (addText.indexOf("Invalid field") != -1) { /* it's present */ }
// OR with regex
if (/Invalid field/.test(addText)) { /* it's present */ }
// OR with case-insensitve regex:
if (/Invalid field/i.test(addText)) { /* it's present */ }
you can use strstr , document # http://www.php.net/manual/en/function.strstr.php
Thanks
I really don't recommend parsing html with regex, but there's no easy way in vanilla javascript.
If you use jQuery, then this is quite a simple task with the :contains selector :
var toSearch = "Invalid field"
elements = $(':contains('+ toSearch +')');
// if we have any elements that contain the searched keywords
if(elements.length) {
// do smth
}

Is it possible to use pages in Magento to have a customizable contact form?

Is it possible to use pages in Magento to have a customizable contact form?
If it's possible:
What action should I call to support this?
Is it possible to use my customize form elements or do i need to use Magento Standards?
Form I'm using to my Page:
<form action="" method="post">
<input type="hidden" name="formID" value="20642064378453">
<div class="form-all">
<ul class="form-section">
<li class="form-line" id="id_8">
<label class="form-label-left" id="label_8" for="input_8"> Full Name </label>
<div id="cid_8" class="form-input"><span class="form-sub-label-container"><input class="form-textbox" type="text" size="10" name="q8_fullName8[first]" id="first_8">
<label class="form-sub-label" for="first_8" id="sublabel_first"> First Name </label></span><span class="form-sub-label-container"><input class="form-textbox" type="text" size="15" name="q8_fullName8[last]" id="last_8">
<label class="form-sub-label" for="last_8" id="sublabel_last"> Last Name </label></span>
</div>
</li>
<li class="form-line" id="id_1">
<label class="form-label-left" id="label_1" for="input_1">
Name<span class="form-required">*</span>
</label>
<div id="cid_1" class="form-input">
<input type="text" class="form-textbox validate[required]" id="input_1" name="q1_name" size="20">
</div>
</li>
<li class="form-line" id="id_3">
<label class="form-label-left" id="label_3" for="input_3">
Email<span class="form-required">*</span>
</label>
<div id="cid_3" class="form-input">
<input type="text" class="form-textbox validate[required]" id="input_3" name="q3_email" size="20">
</div>
</li>
<li class="form-line" id="id_4">
<label class="form-label-left" id="label_4" for="input_4"> Topics </label>
<div id="cid_4" class="form-input">
<select class="form-dropdown" style="width:150px" id="input_4" name="q4_topics">
<option> </option>
<option value="Option 1"> Option 1 </option>
<option value="Option 2"> Option 2 </option>
<option value="Option 3"> Option 3 </option>
</select>
</div>
</li>
<li class="form-line" id="id_5">
<label class="form-label-left" id="label_5" for="input_5"> Other Options </label>
<div id="cid_5" class="form-input">
<div class="form-single-column"><span class="form-radio-item" style="clear:left;"><input type="radio" class="form-radio" id="input_5_0" name="q5_otherOptions" value="Option 1">
<label for="input_5_0"> Option 1 </label></span><span class="clearfix"></span><span class="form-radio-item" style="clear:left;"><input type="radio" class="form-radio" id="input_5_1" name="q5_otherOptions" value="Option 2">
<label for="input_5_1"> Option 2 </label></span><span class="clearfix"></span><span class="form-radio-item" style="clear:left;"><input type="radio" class="form-radio" id="input_5_2" name="q5_otherOptions" value="Option 3">
<label for="input_5_2"> Option 3 </label></span><span class="clearfix"></span>
</div>
</div>
</li>
<li class="form-line" id="id_7">
<label class="form-label-left" id="label_7" for="input_7"> Message </label>
<div id="cid_7" class="form-input">
<textarea id="input_7" class="form-textarea" name="q7_message" cols="40" rows="6"></textarea>
</div>
</li>
<li class="form-line" id="id_2">
<div id="cid_2" class="form-input-wide">
<div style="margin-left:156px" class="form-buttons-wrapper">
<button id="input_2" type="submit" class="form-submit-button">
Submit Form
</button>
</div>
</div>
</li>
<li style="display:none">
Should be Empty:
<input type="text" name="website" value="">
</li>
</ul>
</div>
<input type="hidden" id="simple_spc" name="simple_spc" value="20642064378453-20642064378453">
<script type="text/javascript">
document.getElementById("si" + "mple" + "_spc").value = "20642064378453-20642064378453";
</script>
</form>
Thank You!
The Mage_Contacts_IndexController::postAction() method can process custom contact forms.
The posted data needs to fulfill some basic requirements:
// Excerpt from Mage_Contacts_IndexController::postAction()
$postObject = new Varien_Object();
$postObject->setData($post);
$error = false;
if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
$error = true;
}
if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
$error = true;
}
if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
$error = true;
}
if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
$error = true;
}
if ($error) {
throw new Exception();
}
This code shows you which criteria your form needs to fulfill you the controller action works with your data out of the box.
First of all, the data has to be posted (not sent via GET).
Then a non-empty name field, an email field (with a syntacticly correct email), a non-empty hideit field and a comment field need to be present.
The form needs to be posted to the URL /contacts/index/post.
The best way to generate the URL in Magento would be echo Mage::getUrl('contacts/index/post').
The posted data is made available in the contacts email through the {{ var data}} variable.
The email template can be adjusted through the admin interface under System > Transactional Emails.
By default the template is very basic but can be easily extended. For example, to add the value of a field called q5_otherOptions to the email use `{{var data.q5_otherOptions}} as a placeholder.
The action has to be $this->getUrl('module/controller/action') where module is the name of your custom Module, controller is a Controller in your module, and action is an Action in that controller. You need to make your own custom module for this. You should read the first few articles at http://alanstorm.com/category/magento to learn how to do this.
Here's a post I wrote detailing a very basic custom contact form setup. This may or may not meet your requirements, but if all you want to do is add some additional fields to the contact form, this is a decent enough method.
http://www.magentocommerce.com/boards/viewthread/262835/

Categories