Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8508639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:12:59+00:00 2026-06-11T03:12:59+00:00

I am using jQuery (version 1.8.1) with PHP (version 5.3) to submit a form

  • 0

I am using jQuery (version 1.8.1) with PHP (version 5.3) to submit a form adding an entry into a mySQL database, what is happening is on the first submit everything is fine but for each subsequent submission without a page refresh it adds an additional entry.

In addition I’m also using Bootstrap (version 2.1.1) and the upload widget from Jasny for Bootstrap (version j1a) in the UI. I have not yet connected the upload widget to the processing or submit as I detected the duplication problem when I was implementing it.

Please note that this is a proof of concept system so the code is rough as I’m not going to invest in cleaning it up until the project is confirmed. Due to this, you will notice some inline mySQL queries, I know that this isn’t the best way to do it however it works for the purpose of demonstration and will be cleaned up later. Also as a POC system it is on an internal server currently, I can share the code but cannot show an example site at this time unfortunately.

Now back to the issue, as an example, the first post for “Company 1” has 1 record added for “Company 1”, the second record for “Company 2” adds 2 records for “Company 2”, the third record for “Company 3” adds 3 records for “Company 3” and so on. If I reload the form page in any way (refresh or a new request) the problem restarts from the first submission.

I am using jQuery serialize with ajax to post the data to the PHP processor. I have logged all of the posts being received by the processor and I see the processor is receiving multiple records from the form, I thought it may have been caused by a foreach loop in the PHP but this is not the case.

I have removed the jQuery functions and it works perfectly each time without any duplicates on normal PHP submit.
I have manually processed the entries via jQuery instead of serialize but as there is a dynamic array via PHP I still used serialize on that array, this produced the duplicates as described above.
I have searched the issue for a number of days but cannot find anything definitive to clear up the issue, all suggestions on blogs and forums that looked to be related did not work, I have tried around 10-15 different options.

The combination of all of this leads me to believe the issue is coming from the jQuery serialize and/or ajax functions but my eyes have become glazed each time I look at this code now.

I am also considering placing the form in an external file and reloading it fresh via ajax or cleaning the form setting it back to defaults via jQuery for each new entry required however I do not believe either of these approaches will solve the problem.

Any help is greatly appreciated, thanks in advance for the help!

jQUERY code

<script>
    $(document).ready(function() {
        $('.fileupload').fileupload('name:logo');
        $('.help-inline').hide();
        $("#btn_process").click(function() {
            $('form').submit(function() {
            $('.help-inline').hide();  
            var company_name = $("#company_name").val();  
            if (company_name === "") { 
                $("div#name_group").addClass("error");
                $("span#name_error").show();   
                return false;  
            } 
            var dataString = $('form').serialize();
            $.ajax({
                type: "POST",
                url: "inc/addcompany.php",
                data: dataString,
                success: function(html) {
                    if(html === 'success') 
                    {
                        $('#message')
                        .addClass("label label-success")
                        .css("margin-bottom","20px")
                        .html("<h3>Login successful</h3><p>Company added</p>")
                        .slideDown(1500, function() {});
                    }
                    else 
                    {
                        $('#message')
                        .addClass("label label-important")
                        .css("margin-bottom","20px")
                        .html("<h3>Error</h3><p>There was an error, please check the information and try again</p>")
                        .slideDown(1500, function() {});
                        $("div#name_error").addClass("error"); 
                        $("span#name_error").show(); 
                        $("div#type_error").addClass("error");
                        $("span#type_error").show();   
                        return false;  
                    }
                }
            });
            return false;
        }); 
    });
});
</script>

HTML markup

<form class="form-horizontal" id="add_company" method="POST" action="">  
                <fieldset>
                        <div id="message"></div>
                        <div id="name_group" class="control-group">
                            <label class="control-label" for="company_name">Company name </label>
                            <div class="controls">
                                <input type="text" id="company_name" name="company_name" />
                                <span id="name_error" class="help-inline">This needs to be more than 3 characters</span>
                            </div>
                        </div>
                        <div id="type_group" class="control-group">
                            <label class="control-label">Company type </label>
                            <div class="controls">
                            <?
                            $sql = "SELECT description,id FROM types ORDER BY description";
                            $qry = mysql_query($sql) or die("ERROR: could not get company types => ".mysql_error());
                            while($company_type = mysql_fetch_array($qry)) { 
                                echo '
                                <label class="checkbox inline"><input type="checkbox" name="type[]" value="'.$company_type['id'].'" /> '.$company_type['description'].' </label>';
                            }
                            ?>
                            <span id="type_error" class="help-inline">Please select a minimum of 1 type</span>
                            </div>
                        </div>
                        <div id="website_group" class="control-group">
                            <label class="control-label" for="website">Website </label>
                            <div class="controls">
                                <input type="text" id="website" name="website" placeholder="www.something.com" />
                            </div>
                        </div>
                        <div id="logo_group" class="control-group">
                            <label class="control-label">Logo </label>
                            <div class="controls">
                                <div class="fileupload fileupload-new" data-provides="fileupload">
                                <div class="fileupload-new thumbnail" style="width: 50px; height: 50px;"><img src="/img/50x50.png" /></div>
                                <div class="fileupload-preview fileupload-exists thumbnail" style="width: 50px; height: 50px;"></div>
                                <span class="btn btn-file"><span class="fileupload-new">Select image</span>
                                <span class="fileupload-exists">Change</span>
                                <input type="file" /></span>
                                <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
                            </div>
                        </div>
                </fieldset>
                <input type="hidden" name="action" value="add_company" />
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button class="btn btn-primary" name="btn_process" id="btn_process">Save changes</button>
  </form>

The PHP processor

$error = false;
$error_company_name = false;
$error_type = false;
$error_website = false;
$company_name = $_REQUEST['company_name'];
$type = $_REQUEST['type'];
$website = $_REQUEST['website'];
$logo = $_REQUEST['logo'];

if(empty($company_name)) {
    $error = true;
    $error_company_name = true;
}
include_once('db.php');
$sql = "SELECT description,id FROM company_types";
$qry = mysql_query($sql) or die("ERROR: could not get company types => ".mysql_error());
$type_count = 0;
while($array = mysql_fetch_array($qry)) {
    $type_count = $type_count+1;
}
if($type_count == 0) {
    $error = true;
    $error_type = true;
}
$ic = 0;
foreach($_REQUEST['type'] as $item) {
    $ic = $ic+1;
}
if($ic == 0) {
    $error = true;
    $error_type = true;
}
if(isset($website) && $website != ' ') {
    $url = 'http://'.$website;
    if(!filter_var($url, FILTER_VALIDATE_URL)) {
        $error = true;
        $error_website = true;
    }
}
if($error == false) {
    $sql = "INSERT INTO company_list (name,website,logo) VALUES('$company_name','$website','$logo')";
    $qry = mysql_query($sql) or die ("ERROR: could not add company => ".mysql_error());
    $company_id = mysql_insert_id($link);
    if($company_id == '' || $company_id == null || empty($company_id)) {
        echo 'fail';
        exit;
    } 
    foreach($_REQUEST['type'] as $company_type) {
        $sql = "INSERT INTO companies_types (companies_id,type_id) VALUES('$company_id','$company_type')";
        $qry = mysql_query($sql) or die("ERROR: could not link company type: => ".mysql_error());
    }
    echo 'success';
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:13:01+00:00Added an answer on June 11, 2026 at 3:13 am

    Add $('form').unbind('submit'); immediately above this line: $('form').submit(function().

    I found this solution here: https://stackoverflow.com/a/668354/300575

    Note: I verified that this works by copying your code and testing it on my server.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a news feed that has been developed using PHP, MySQL, and jQuery.
I am using jQuery 1.7.2 version and I am submitting my form using Ajax
I'm creating a web chat page using PHP and jQuery, and I am adding
This is my second attempt at a php contact form using Jquery and php
I'm using jQuery version 1.7.1 from Google CDN and have the following code: $(menuInstance).delay(2000).removeClass('loading').html(ul);
i am using jquery version 4.x and i have implemented column chooser on my
Using jQuery on() version 1.7. I bind my events usually like this: $(.foo).on(click, function()
ok, im using Jquery (edge) version, and bootstrap 2.0, and i have this code:
My drupal version is 6 and currently running jquery version 1.6.2. I am using
i am doing a mobile version of a website using JQuery Mobile and JSON

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.