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 5993523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:41:14+00:00 2026-05-22T23:41:14+00:00

I am trying to use jQuery-AJAX to submit the data in my form to

  • 0

I am trying to use jQuery-AJAX to submit the data in my form to my controller (index.php) where it is processed by PHP and inserted via PDO into the database if valid. Once the code is inserted into the database, the div where the form previously existed should be replaced by the contents of another page (newpage.php). The original page should not be refreshed upon submitting of the form, only the div where the form previously existed should be refreshed. There is a particular problem with my code, although I can’t seem to find where the issue is at:

Here is my jQuery:

<script type="text/javascript">

    function processForm() {
    var action= $('#action').val();
    var data = $('#data').val();
    var dataString = 'action=' + action + '&data=' + data;

$.ajax ({
    type: "POST",
    url: ".",
    data: dataString,
    success: function(){
            $('#content_main').load('newpage.php');
        }
    });
}
</script>

Here is my HTML: (As a side note, I noticed that when I take the “return false;” out of the HTML, that the form will submit to my database, but the whole page also reloads – and is blank. When I leave the “return false;” in the HTML, the newpage.php loads correctly into the div, but the data does not make it into the database)

<form action="" method="post" onsubmit="processForm();return false;">

    <input type="hidden" name="action" value="action1" />
    <input type="text" name="data" id="data" />

    <input type="submit" value="CONTINUE TO STEP 2" name="submit" />

</form>

Here is my PHP:

<?php
$action = $_POST['action'];

switch ($action) {
    case 'action1':
        $data = $_POST['data'];

        pdo ($data);

    exit;
}
?>

I feel like I am making a silly mistake somewhere, but I just can’t put my finger on it. Thanks for any assistance you can provide!

SOLUTION (via Jen):

jQuery:

<script type="text/javascript">
    function processForm() {
        var dataString = $("#yourForm").serialize();

    $.ajax ({
    type: 'POST',
    url: ".",
    data: dataString,
    success: function(){
            $('#content_main').load('newpage.php');
          }
        });
    return false;
    });
</script>

HTML:

<form id="yourForm">

    <input type="hidden" name="action" value="action1" />
    <input type="text" id="data" name="data" />

    <input type="submit" value="CONTINUE TO STEP 2" name="submit" />

</form>

PHP:

<?php
    $action = $_POST['action'];

    switch ($action) {
        case 'action1':
            $data = $_POST['data'];

            pdo ($data);

        exit;
    }
?>

What I learned: Use the .serialize() jQuery method if it is an option, as it will save a bunch of time writing out the var for each form value and .serialize() does not typically make mistakes sending the info to php.

  • 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-05-22T23:41:15+00:00Added an answer on May 22, 2026 at 11:41 pm

    Give this a try:

     $("#yourForm").submit(function(){
    
    
      // Could use just this line and not vars below      
      //dataString = $("#yourForm").serialize(); 
    
      // vars are being set by selecting inputs by id but id not set in form fields
      var action= $('#action').val(); // value of id='action' 
      var data = $('#data').val();    // value of id='data'
      var dataString = 'action=' + action + '&data=' + data;
      // dataString = 'action=&data=' so nothing is posted to db
      // because input fields cannot be found by id
      // fix by adding id fields to form fields
    
       $.ajax ({
       type: "POST",
       url: ".",
       data: dataString,
       success: function(){
            $('#content_main').load('newpage.php');
        }
       });
    return false; 
    });
    

    And change the form to (I added the id attributes):

    <form id="yourForm">
    <input type="hidden" id="action" name="action" value="action1" />
    <input type="text" id="data" name="data" id="data" />
    
    <input type="submit" value="CONTINUE TO STEP 2" name="submit" />
    
    </form>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to use jquery and ajax to submit a form and show the
I'm trying to use jquery.Ajax to post data to an ASP.NET MVC2 action method
So I am trying to use jQuery to insert data from an ajax call.
I'm trying to use form submit with jquery without a page refresh as demonstrated
I have a form I'd like to submit as JSON via jQuery AJAX, so
I am trying to submit a form using jQuery's .ajax() function. It seems like
I'm trying to submit a form with AJAX through jQuery: $('.submit input').click(function() {return false;});
I'm trying to use the jquery validate plugin to validate a form and submit
I'm trying to use the jQuery forms plugin to dynamically submit a form on
I am trying to use jQuery's ajax functionality to update data from a web

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.