0

I code am html page with javascript my code is given below

    <input type="text" id="name" name="name" value="" size="40" class="form-control"placeholder="Name*" />

    <input type="text" id="email" name="email" value="" size="40" class="form-control" placeholder="E-mail*" />

    <input type="text" id="phone" name="email-414" value="" size="40" class="form-control" placeholder="Phone*" />

    <textarea id="message" name="textarea" cols="40" rows="10" class="form-control" placeholder="Message"></textarea>

    <input type="submit" value="Send Enquiry" id="submit_mail_cont" class="btn btn-success" />

and javascript is (using jQuery)

<script type="text/javascript">
        $("#submit_mail_cont").click(function(){alert('Clicked);var name = $("#name").val();var email   = $("#email").val();var phone   = $("#phone").val();
                    var message = $("#message").val();
                    console.log(name);
                    console.log(email);
                    console.log(phone);
                    console.log(message);
                    $("body").css('cursor', 'wait');
                    if (name == "" || email == "" || phone == "" || message == "") {
                        alert("Fill All fields");
                    }else{
                        $.ajax({
                            url: 'http/url/to/my/page.php',
                            type: 'POST',
                            dataType: 'json',
                            data: {"name": name,"email": email,"phone": phone,"message": message},
                        })
                        .done(function() {
                            alert("Mail sent");
                        })
                        .fail(function() {
                            alert("Fail");
                        })
                        .always(function(){
                            $("body").css('cursor', 'auto');
                        });
                    };
                });
        </script>

the above code is working fine in my local system but while I uploaded it to server side It doesn't word. I got this error while I was debugging my code Uncaught SyntaxError: Invalid or unexpected token java script on js start tag

I didn't find error and solution.

11
  • 1
    Did you check the file encoding? Commented Jun 23, 2017 at 12:43
  • on the server? @AdemirConstantino Commented Jun 23, 2017 at 12:44
  • How are you loading jQuery? Locally or through a CDN? Commented Jun 23, 2017 at 12:45
  • @chigs, yes, it almost seems like this is an UTF-8 issue. What editor are you using on your local machine? Commented Jun 23, 2017 at 12:45
  • 1
    There's a missing quote in the first alert Commented Jun 23, 2017 at 12:46

2 Answers 2

4

Remove ; from else's closing curly brackets };

$("#submit_mail_cont").click(function()
{
    .....
    if (name == "" || email == "" || phone == "" || message == "") {
        .....  
    }
    else
    {
        .....
    // }; here you have semicolon 
    }  // remove that semicolon
});

Other error as suggested by @Reshma in the comment is:

your alert missing an ' apostrope – @Reshma

Sign up to request clarification or add additional context in comments.

Comments

2

You missed closing quote in alert('Clicked);

Fix that and it will work as expected.

$("#submit_mail_cont").click(function() {
    alert('Clicked');
    var name = $("#name").val();
    var email = $("#email").val();
    var phone = $("#phone").val();
    var message = $("#message").val();
    console.log(name);
    console.log(email);
    console.log(phone);
    console.log(message);
    $("body").css('cursor', 'wait');
    if (name == "" || email == "" || phone == "" || message == "") {
        alert("Fill All fields");
    } else {
        $.ajax({
                url: 'http/url/to/my/page.php',
                type: 'POST',
                dataType: 'json',
                data: {
                    "name": name,
                    "email": email,
                    "phone": phone,
                    "message": message
                },
            })
            .done(function() {
                alert("Mail sent");
            })
            .fail(function() {
                alert("Fail");
            })
            .always(function() {
                $("body").css('cursor', 'auto');
            });
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="name" name="name" value="" size="40" class="form-control" placeholder="Name*" />

<input type="text" id="email" name="email" value="" size="40" class="form-control" placeholder="E-mail*" />

<input type="text" id="phone" name="email-414" value="" size="40" class="form-control" placeholder="Phone*" />

<textarea id="message" name="textarea" cols="40" rows="10" class="form-control" placeholder="Message"></textarea>

<input type="submit" value="Send Enquiry" id="submit_mail_cont" class="btn btn-success" />

4 Comments

There is a semicolon ; that has nothing to do here on the second last line of your script (closing else statement).
I tried it but nothing happened. Now i can see the clicked alert but nothing else working. Also the same error is show on same place. @tushar
For me it's trying to send data to .php page. If all data is filled and show 'Fail' if any field is missing.
Yes I'm trying to send data to php. But it shows nothing.. Not success not fail. Data is not being sent too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.