I have JavaScript function that is supposed to called OnClientClicking but is getting called on page load.
function SaveClientClicking(sender, eventArgs)
{
var FirstName = $find("<% = FN.ClientID %>");
var FNtext = FirstName._value;
var LastName = $find("<% = LN.ClientID %>");
var LNtext = LastName._value;
var Email = $find("<% = email.ClientID %>");
var Emailtext = Email._value;
if (FNtext == "")
{
alert("First Name can't be blank");
eventArgs.set_cancel(true);
}
else if (LNtext == "") {
alert("Last Name can't be blank");
eventArgs.set_cancel(true);
}
else if (Emailtext == "") {
alert("Email address can't be blank");
eventArgs.set_cancel(true);
}
}
And another JavaScript function that is supposed to be called OnClientItemSelected but also is getting called on page load.
function Selecting(sender, eventArgs)
{
var Country = $find("<% = Country.ClientID %>");
var CountryDR = Country.get_selectedItem().get_text();
var State = $find("<% = State.ClientID %>");
if (CountryDR != "United States")
{
State == "Outside of US";
}
}
I am pretty new to JavaScript so any suggestions would be of great help.
Your brackets dont add up i think that might be the problem
You have the ending brackerts in both functions missed, please correct it and check.
Related
</script>
<button onclick="password1()">Password</button>
<script>
function password1(){
var pass = prompt ("Password"); -- Pasword Prompt
var wo = "No"; -- If Wrong
var rd = "Yes"; -- If Correct
var awn = 123; -- Code
if (pass == 123) { -- Check the password
document.write(rd);
}
}
</script>
This is what i tried but for a reason nothing appears Please help me
Wrong comments
No "else" statement
Variable "awn" defined but never used
<script>
function password1(){
var pass = prompt("Password"); // Pasword Prompt
var wo = "No"; // If Wrong
var rd = "Yes"; // If Correct
var awn = 123; // Code
if (pass == awn) { // Check the password
document.write(rd);
} else {
document.write(wo)
}
}
</script>
so basically, when someone clicks the submit input, I want to retrieve values from various inputs and spans using javascript, then send it to php using $post to then send it by email. I have already tried to delete all the javascript code (except the preventdefault and the click function) and replacing it by a alert, and when I click submit, the alert does execute as it should. But now i'm trying to figure out how to make retrieve all this data and send it by email.
I have a live example here, if you want to see what i'm trying to do.
Thanks
<script type="text/javascript">
$("#submit").click(function (e) {
e.preventDefault();
var PrimaryParentName;
var SecondaryParentName;
var PrimaryEmail;
var SecondaryEmail;
var PrimaryPhone;
var SecondaryPhone;
var Address;
var ChildName;
var ChildBirth;
var ChildAllergies;
var ChildSchool;
var ChildLevel;
var UniformSize;
var PreferedPositions;
PrimaryParentName = document.getElementById("first_parent_name")
.value;
if (document.getElementById("second_parent_name").value ==
null || document.getElementById("second_parent_name")
.value == "") {
SecondaryParentName = 'N/A';
} else {
SecondaryParentName = document.getElementById(
"second_parent_name").value;
}
SecondaryEmail = document.getElementById("first_email")
.value;
if (document.getElementById("second_email").value ==
null || document.getElementById("second_email")
.value == "") {
SecondaryEmail = 'N/A';
} else {
SecondaryEmail = document.getElementById(
"second_email").value;
}
PrimaryPhone = document.getElementById("first_phone")
.value;
if (document.getElementById("second_phone").value ==
null || document.getElementById("second_phone")
.value == "") {
SecondaryPhone = 'N/A';
} else {
SecondaryPhone = document.getElementById(
"second_phone").value;
}
Address = document.getElementById("address")
.value;
ChildName = document.getElementById("child_name")
.value;
ChildBirth = document.getElementById("child_date")
.value;
ChildAllergies = document.getElementById("child_allergies")
.value;
ChildSchool = document.getElementById("school")
.value;
ChildLevel = document.getElementById("uniform_size")
.value;
UniformSize = document.getElementById("leveldescription")
.innerHTML;
PreferedPositions = document.getElementById("Goalie")
.value;
alert(PreferedPositions + UniformSize + ChildLevel);
$.post('registration.php', {
PostPrimaryParentName: PrimaryParentName,
PostSecondaryParentName: SecondaryParentName,
PostPrimaryEmail: PrimaryEmail,
PostSecondaryEmail: SecondaryEmail,
PostPrimaryPhone: PrimaryPhone,
PostSecondaryPhone: SecondaryPhone,
PostAddress: Address,
PostChildName: ChildName,
PostChildBirth: ChildBirth,
PostChildAllergies: ChildAllergies,
PostChildSchool: ChildSchool,
PostChildLevel: ChildLevel,
PostUniformSize: UniformSize,
PostPreferedPositions: PreferedPositions
}, function () {});
});
</script>
<?php
$PrimaryParentName=$_POST['PostPrimaryParentName'];
$SecondaryParentName=$_POST['PostSecondaryParentName'];
$PrimaryEmail=$_POST['PostPrimaryEmail'];
$SecondaryEmail=$_POST['PostSecondaryEmail'];
$PrimaryPhone=$_POST['PostPrimaryPhone'];
$SecondaryPhone=$_POST['PostSecondaryPhone'];
$Address=$_POST['PostAddress'];
$ChildName=$_POST['PostChildName'];
$ChildBirth=$_POST['PostChildBirth'];
$ChildAllergies=$_POST['PostChildAllergies'];
$ChildSchool=$_POST['PostChildSchool'];
$ChildLevel=$_POST['PostChildLevel'];
$UniformSize=$_POST['PostUniformSize'];
$PreferedPositions=$_POST['PostPreferedPositions'];
$to='email#gmail.com';
$subject= 'Nouvelle inscription pour le CSEO';
$message="<span style='font-size: 26px'><b>Contact information</b></span>"."Primary parent's name: ".$PrimaryParentName."\n"."Primary parent's email: ".$PrimaryEmail."\n"."Primary parent's phone number: ".$PrimaryPhone."\n\n"."Secondary parent's name: ".$SecondaryParentName."\n"."Secondary parent's email: ".$SecondaryEmail."\n"."Secondary parent's phone number: ".$SecondaryPhone."\n\n"."Address: ".$Address."\n <hr> \n"."<span style='font-size: 26px'><b>Child's information</b></span>"."\n"."Child name: ".$ChildName."\n"."Child's date of birth: ".$ChildBirth."\n"."Allergies: ".$ChildAllergies."\n"."Child's school: ".$ChildSchool."\n"."Child's level of experience: ".$ChildLevel."\n"."Prefered positions: ".$PreferedPositions."\n"."Uniform size: ".$UniformSize;
mail($to, $subject, $message);
?>
Alright I solved it, the problem was with the if in before the $post.
if (document.getElementById("second_phone").value ==
null || document.getElementById("second_phone")
.value == "") {
SecondaryPhone = 'N/A';
} else {
SecondaryPhone = document.getElementById(
"second_phone").value;
}
I don't know what the exact issue was but it works without it and i'm fine without them.
My JavaScript inside the head tag:
<script type="text/javascript">
function over1() {
var img1 = document.getElementById("1").src;
document.getElementById("big").src = img1;
}
function out() {
document.getElementById("big").src = "http://icons.iconarchive.com/icons/paomedia/small-n-flat/1024/shop-icon.png";
}
function london() {
var city = document.getElementById("city").value;
var check = city.toLowerCase();
var province = document.getElementById("province").value;
if (check == "london" && province == "ON") {
alert("Visit our company travel store at Masonville Mall!");
}
}
function checkinput() {
var email = document.contest.email.value;
var emailcheck = email.search("#");
if (!document.contest.name.value) {
alert("Enter a name!")
} else {
alert("Thank You " + document.contest.name.value + " " + document.contest.lastname.value + " For Entering The Contest!")
window.open(calculator.html,'_blank');
}
}
</script>
I have the simple JavaScript inside the HTML file, but Chrome won't read it. In Inspector View, it throws ReferenceErrors for all my functions. Please help.
Why do you say that, those are all functions, nothing is invoked from these functions. call the functions and see if they are invoked correctly or not
checkinput();
over1();
/* the rest of them */
Iam trying to update Div text based on js variable null or not.
This is the code used for checking var userName is null or not.
If its null i dont want to do anything.But if userName is not null then i need to append ',' to existing text inside div .
$("document").ready(function (){
var userName = "";
if (userName == undefined || userName == null) {
alert('empty');
} else {
alert('not empty');
var div = document.getElementById('title b');
div.innerHTML = div.innerHTML + ',';
}
});
my html of the div
<div id="title" class="box">
<b>Welcome</b>
</div>
For example if userName is not null then i want div text as "Welcome ,"
if userName is null then i want div text as "Welcome"
Since you're using jQuery already...
$(document).ready(function (){
var userName = ""; // this will never get into the following if statement, always else
if (userName == undefined || userName == null) { // use if (!userName) as shorthand.
alert('empty');
} else {
alert('not empty');
var div = $('#title > b');
div.text(div.text() + ',');
}
});
You're using an odd mix of POJS and jQuery here. Here's a purely jQuery solution:
$("document").ready(function (){
var userName = "";
if (userName == undefined || userName == null) {
alert('empty');
} else {
alert('not empty');
$('#title b').append(',');
}
});
It's a little odd that you're declaring userName as an empty string within your function - the first if condition will never execute.
A more jQuery answer, if you are interested:
var userName = "";
//var userName = "User"; // for testing
//var userName; // for testing
//var userName = null; // for testing
$(document).ready(function (){
var greeting = !!userName ? "Welcome, "+ userName + "!" : "Welcome";
$("#title b").html(greeting);
})
jsFiddle
I am not much of a javascript programmer, I am learning so please forgive the stupid mistakes.
I am trying to save customer details using ajax (its not the jquery ajax), which returns another form for advanced details. I am using a jquery plugin for validation of input boxes. But the validation doesn't work on the form returned by ajax.
Ajax code:
function createcustomer() {
var firstname = _("firstname").value;
var lastname = _("lastname").value;
var email = _("email").value;
var phone = _("phone").value;
var status = _("status");
if (firstname == "" || lastname == "" || email == "" || phone == "") {
status.innerHTML = "Fill out all of the form data";
} else {
_("signupbtn").style.display = "none";
status.innerHTML = 'please wait ...';
var ajax = ajaxObj("POST", "save-customer.php");
ajax.onreadystatechange = function () {
if (ajaxReturn(ajax) == true) {
if (ajax.responseText != "failure") {
_("signupform").innerHTML = "";
_("advancedform").innerHTML = ajax.responseText;
} else {
window.scrollTo(0, 0);
_("signupform").innerHTML = "Error Occured";
}
}
}
ajax.send("firstname=" + firstname + "&lastname=" + lastname + "&email=" + email + "&phone=" + phone);
}
}
function ajaxObj(meth, url) {
var x = new XMLHttpRequest();
x.open(meth, url, true);
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x) {
if (x.readyState == 4 && x.status == 200) {
return true;
}
}
I have tested the ajax return code thouroughly and the jquery works independently on that form.
This is how I initiate the jquery validation plugin :
$(document).ready(function() {
$('form').validationEngine();
});
I tried putting the exact code in the createcustomer() function to reload the jquery function but it doesnt work. Is there any way I can call that jquery validation function in the ajax function?
Any help will be appreciated.
Regards
Priyanshu
I'm suspecting that your validation is trying to find a form that doesnt exists at the execution time, try putting the validation binding when you have sure that the form exists in your DOM. To achieve this you should put the validation binder:
$('form').validationEngine();
...inside the callback after the ajax loads some HTML data in the document. Like this:
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
if(ajax.responseText != "failure"){
_("signupform").innerHTML = "";
_("advancedform").innerHTML = ajax.responseText;
$('#ajaxreturnform').validationEngine('attach'); //Here
} else {
window.scrollTo(0,0);
_("signupform").innerHTML = "Error Occured";
}
}
}
I've looked around here and found this post Attach Validation the accepted answer may inlight your problem also. Try to put the 'attach' command as a parameter to your binder as well as the #id of your form.