Writing a page with use of $.cookie to show a form upon submit in a multi form page. I have seen similar questions go unanswered when it got to this specific code. I have read and read, seems like this should work. Yes, very new to jquery, 1 week.
I have container divs (class) each with an p toggle to show/hide each form. Upon submit I want the form to remain .show() so that errors or even response will be seen. p must be clicked to .hide form; so a person could open all the forms.
I know there are much more complicated ways to do this. I just want to learn the simple, possibly long hand version so help me understand what is going on. I added a number of alerts for hints. Some reason loops back to undefined.
Sorry I am so 'green'. I am sure this is too simple for most here.
css:
.set {
}
.set form {
display: none;
}
.set p {
color: #996600;
cursor: pointer;
}
.set p.hover {
background-color: #00CCCC;
}
the html:
<div class="set"><p>Click</p>
<form id="form1">
<div id="here" style="text-align:center; border:1px solid black; width:60px;">form 1</div>
<button id="form1button" class="button">BUTTON form1</button>
</form>
</div>
<div class="set"><p>Click2</p>
<form id="form2" >
<div id="here" style="text-align:center; border:1px solid black; width:60px;">Click</div>
<button id="form2button" class="button">BUTTON form2</button>
</form>
</div>
<div class="set"><p>Click3</p>
<form id="form3" >
<div id="here" style="text-align:center; border:1px solid black; width:60px;">Click</div>
<button id="form3button" class="button">BUTTON form3</button>
</form>
</div>
the jquery:
if($.cookie('toogle')=== undefined) {
$('.set form#form1').show();
alert('undefined');
if ($.cookie('toogle') == 'form1') {
$('.set form#form1').show();
$('.set form#form2').hide();
$('.set form#form3').hide();
} else if ($.cookie('toogle') == 'form2') {
$('.set form#form2').show();
$('.set form#form1').hide();
$('.set form#form3').hide();
} else if ($.cookie('toogle') == 'form3') {
$('.set form#form3').show();
$('.set form#form1').hide();
$('.set form#form2').hide();
} else {
$('.set form#form1').show();
$('.set form#form2').hide();
$('.set form#form3').hide();
};
alert($.cookie('toggle'));
$('#form1button').on('click', function() {
$.cookie('toggle', 'form1');
alert('form1 button!!!');
});
$('#form2button').on('click', function() {
$.cookie('toggle', 'form2');
alert('form2 button!!!');
});
$('#form3button').on('click', function() {
$.cookie('toggle', 'form3');
alert('form3 button!!!');
});
$('.set p').click(function() {
$(this).toggleClass('active').next().slideToggle('slow');
});
Ok so overall we will have a page with the forms, and a php processing page. I explained most of it in the comments. Let me know if you have any questions!
Here is a demonstration page
This is a quick validation to see if the value is empty or not, you can do whatever validating you need to
Page 1 - send-ajax-form.php - CSS
Page 1 - send-ajax-form.php - HTML
Page 1 - send-ajax-form.php - Script (Buttons)
Page 1 - send-ajax-form.php - Script (Ajax Calls)
Page 2 - process-ajax-form.php - PHP