Submit a form to the same page without reloading and toggle the visibility of a div after submission

Is there a way to submit a form without refreshing the page and hiding the current div while showing a hidden one?

I attempted to use the following code, but it ends up submitting the form and reloading the page.

<form method="post" name="searchfrm" action="schedule.php" >
      <div><input type="text" placeholder="Patient Lastname" class="pName" id="pName" name="pName" /></div></br>
      <div><input type="text" placeholder="Surgeon Name" class="sName" id="sName" name="sName" /></div></br>
       <div>
          <span><input type="text" placeholder="mm" class="mm" name="mm" /></span>
          <span><input type="text" placeholder="dd" class="dd" name="dd" /></span>
          <span><input type="text" placeholder="yyyy" class="yyyy" name="yyyy" /></span>
          <span><input type="button" class="sButton" id="sButton"/></span>
      </div>

 <!-- Search Results -->
<div id="searchDiv" class="searchDiv" style="display:none;">
<?PHP
$dateVal = $_POST["yyyy"]-$_POST["mm"]-$_POST["dd"];
$sName = $_POST["sName"];
$pName = $_POST["pName"];
echo "dateVal".$sName;
 $ser_val=@mysql_query("SELECT * FROM issio_patient_procedures WHERE procedure_date='$dateVal' AND asc_id='$ascId' AND surgeon_name='".$sName."' ");
  if(@mysql_num_rows($ser_val)>0)
    {
        $se_count=0;
        while($se_count_det=@mysql_fetch_assoc($ser_val))
        {
            $se_count++;
            echo $se_count_det["surgeon_name"];
        }
    }
?>
</div>
 <!-- Search Results End -->

<div class="scroll" id="scroll" style="display:block;">
   <div class="dateHeader">
      Some content....
   </div
</div

Answer №1

One common solution to performing an asynchronous task is utilizing an Ajax Request.

A suggested approach is using the jQuery method:

       $.ajax({
 url:'dataProcessingPage.php',  
     data: { pName:$("#pName").value, sName:$("#sName").value, .....},
     type:'POST',
     contentType: 'application/json; charset=utf-8', 
     dataType: 'json',  
 success:function(){

      $("#formDiv").hide(); 
       },

});

This code will send your form data to dataProcessingPage.php.

Answer №2

Delve into the world of Javascript and AJAX in combination with PHP. I won't provide you with all the code, but this guidance should steer you in the right path.

Consider "AJAX" as a method for loading fragments of web pages or data strings to be processed by Javascript, allowing for the refreshing of specific elements on the existing DOM without reloading the entire page. This is the concept; now it's up to you to implement it in your project.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript JQuery alert popup fails to display

I have been attempting to create an alert pop-up using JQuery that will display when a user clicks on submit without filling out the form. However, when I test my code by leaving the form empty and clicking 'submit', the page just refreshes with ...

Neo4j PHP Graphaware error: '400 Bad Content-Type header' encountered

When running the following test case (assuming correct password) <?php require_once "vendor/autoload.php"; use GraphAware\Neo4j\Client\ClientBuilder; $client = ClientBuilder :: create() -> addConnection("default", "http:/ ...

Building and executing an Angular 2 program on the Windows platform: Step-by-step guide

After successfully installing npm and related packages including TypeScript and Angular 2, I am encountering difficulties running Angular 2 in my browser on a Windows platform. Can someone provide a step-by-step guide to help me create and run Angular 2 ...

Does anyone else have a peculiar problem with css width?

Either I have been designing web pages for an extensive period of time without taking a break or something truly bizarre occurred. <div style="background-color:#0F0; margin:5px; height:5px;"></div> This will generate a lengthy bar with a 5-pi ...

Tips for implementing Bootstrap pagination in VueJS 2

Here is how my website appears: Jobs.vue: <div id="jobs" class="job-item" v-for="(item, index) in showJobs" :key="index" > <router-link ...

The animated loading image is taking an eternity to actually load

My website is loaded with heavy front-end features and I'm using a loading gif to help with the loading process. However, I've noticed that in Safari, there is a delay before the gif loads after the background does, which takes away from the inte ...

Having trouble with JQuery toggle()? Need some assistance with fixing it?

My attempts to utilize JQuery toggle functionality have been unsuccessful. The sliding up and down animation is not smooth and instead happens very quickly. I aim to achieve a sliding effect in my code similar to this example (Please refer to Website Des ...

Utilize API or alternative methods for analyzing Instagram data

My master's thesis requires me to dissect multiple Instagram profiles, each containing over 1000 posts. I am in need of a comprehensive list including the following details: Type of Post (Image, Multi Image, Video) Description Likes Comments (total c ...

Tips for customizing the time selector in material-ui-time-picker

Is there a way to enable keyboard input control for the material-ui-time-picker? Currently, it only allows editing when clicking on the clock interface. Here is a snippet of my code: import React, { Component } from "react"; import { TimePicker } from " ...

The PHP-based registration process is malfunctioning

Trying to simply register, but I'm encountering an issue where the file is named Login.html/.php instead of just Login.html. Below is my HTML form in Login.html: <!DOCTYPE html> <html> <head> <title>Register/ Login</title&g ...

Guide on implementing two submission options in an HTML form using JavaScript

Currently, I am working on a form that includes two buttons for saving inputted data to different locations. However, I am facing an issue with the functionality of the form when it comes to submitting the data. Since only one submit function can be activa ...

What is the process for modifying the path and polyline in an SVG file?

I have a pair of SVG icons that I am working with. Recent Update: Icon One Icon Two One question that I have is whether it is possible to create both icons using the same HTML markup? For instance, can I use something like: <g> <circle ... ...

Extracting IDs, classes, and elements from a DOM node and converting it into a string format

Can someone please guide me on how to extract the DOM tree string from an element? Let's consider this HTML structure: <div> <ul id="unordered"> <li class="list item">Some Content</li> </u ...

Incorporate a new style into several previous slides using the Slick carousel feature

I'm attempting to utilize the Slick carousel to create a timeline. My goal is for the previous slides to remain colored as you progress through the timeline, and turn grey when you go back. I've tried using onAfterChange and onBeforeChange, but I ...

Only conceal the table column if space is limited

A scenario involves an HTML table with two columns that need to be displayed fully, with the second column aligned to the right. Currently, this has been achieved by setting the width of the first column to 100%. However, a challenge arises where the words ...

Issue with Next-Auth getServerSession failing to fetch user data in Nextjs 13.4 API Route

Having an issue with accessing user session data in a Next-Auth/Nextjs 13.4 API Route. I've set up the JWT and Session callback, but the user data defined in the callback function isn't translating correctly to what getServerSession is fetching i ...

The jQuery.ajax request encounters issues within a Chrome extension

I'm in the process of migrating one of my Firefox browser extensions to Chrome, and I've encountered an issue related to an AJAX query. The code snippet provided functions correctly in the Firefox extension but fails with a status of "0" when exe ...

DIV size issue resolved

Content within a div is surpassing the set fixed size determined by CSS. <style> #fixeddiv { position: fixed; margin: auto; max-height: 300px; max-width: 700px; } </style> <div id="fixeddiv"> <div id="M77 ...

The update query inadvertently modifies all entries in the database rather than targeting a specific one

Having recently started using the Zend framework, I am encountering an issue when trying to update data in the database and grid. Instead of updating a specific row, all rows are being updated. Can someone please assist me with this? Below is my controlle ...

Issue with Ajax communication to PHP script causing data not to be sent

I am developing a web application that functions as an extensive form which will later be converted into a PDF document. Upon submission of the form, all input data is sent to a PHP file where they are stored as variables to be included in the PDF. Some of ...