简体   繁体   中英

Form not submitting when clicking submit button

I added some Javascript validation to a form on my website. The site connects to the DB just fine, however when you click on the submit button, no action is taken.

This is the form located in the footer of the page:

<div id="add_restaurant" class="full-shadow">
    <h4>Add a Restaurant</h4>

    <form name="submitForm" action="<?php echo $pageName ?>" method="POST"> 
        <ul class="radio_list" id="category">
            <li>Category:</li>
            <li><label class="radiobtn"><input name="category" type="radio" value="healthy" id="is_healthy" checked="checked"/>Healthy</label></li>
            <li><label class="radiobtn"><input name="category" type="radio" value="unhealthy" id="is_unhealthy"/>Unhealthy</label></li>
        </ul>
        <br />
        <label>Name:</label>&nbsp;<input name="rest_name" id="rest_name" type="text" autocomplete="off"/>
        <input id="submit" type="submit" name="submit" value="Submit">
    </form>

    <p id="add_thanks"></p>
</div>

I imagine the problem is something simple I'm over-looking. Can anyone help?

EDIT: I've found that the issue is here in my JS validation:

$('#submit').off('click').on('click', function(){
    if($('#rest_name').val() === ""){
        $('#add_thanks').html("Please type in a restaurant name.");
    }
    return false;
});

If I disable this code, my form works. What is causing the problem in this JavaScript code?

您不打印$ pageName:

<form name="submitForm" action="<?php echo $pageName ?>" method="POST">

You JavaScript validation is cancelling EVERY click, not just when there is an error.

$('#submit').off('click').on('click', function(){
    if($('#rest_name').val() === ""){
        $('#add_thanks').html("Please type in a restaurant name.");
        return false;  //<-- add this
    }
    //return false;  <-- remove this
});

两个地方,第一个是在表单的操作部分中打印$ pageName,第二个是在操作页面中定义$ submit。

This is what you wrote

<form name="submitForm" action="<?php $pageName ?>" method="POST"> 

You need to print that $pageName variable.

<form name="submitForm" action="<?php print($pageName); ?>" method="POST"> 

It might looks silly. But try this:

Remove all your scripts. And just run the below line on your onclick event.

$("form").submit();

Your form won't be submitted.

Rename the "id" and "name" of your button:

<input id="btnSubmit" type="submit" name="btnSubmit" value="Submit">

Remove all your scripts. And just run the below line on your onclick event.

$("form").submit();

Your form will be submitted.

"You should not use 'submit' as your id and name"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM