Validation in PHP and Javascript is only partially effective

I encountered an issue with my form validation setup that utilizes JavaScript, Ajax, and PHP. While the errors are correctly displayed when the form is filled incorrectly, I am unable to submit the form even if there are no errors. Clicking the submit button simply keeps the page static without any action. In the console, I received an error upon submission: Failed to load file:///C:/Users/ilona/Desktop/BootstrapLandinPage/send.php: Cross origin requests are only supported for specific protocol schemes. My project runs on XAMPP.

Below is a snippet of my HTML code:

<section class="inspiration" id="three">
            <div class="overlay">
                <div class="container">
                    <div class="row">
                        <article class="col-md-12 text-center">
                            <div class="intermediate-container">
                                <div class="heading">
                                    <h2>Have a question? Contact me.</h2>
                                </div>
                                 ... <!-- Truncated for brevity -->
        <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
<script src="js/validateform.js" ></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" ></script>

My script in validateform.js:

..

This is how my send.php looks like:

$error_msg = array();
$success_msg = array();
$data = '';
... <!-- Omitted for conciseness -->


function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

Answer №1

Opening the file directly may not work unless configured in Apache. The file appears to have no connection to the XAMPP web server.

To resolve this issue, follow these steps:

  1. Copy the BootstrapLandinPage directory to c:/xampp/htdocs

  2. Add a virtual host to XAMPP

Edit the file located at

C:\xampp\apache\conf\extra\httpd-vhosts.conf
and insert the following snippet, customized to your setup.

<VirtualHost 127.0.0.2:80>
    ServerName playground.localhost
    ServerAlias playground.localhost
    DocumentRoot c:/xampp/htdocs/your_website
    <Directory "c:/xampp/htdocs/your_website/">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>
  1. Restart Apache in the XAMPP control panel (click XAMPP icon at the bottom right on Windows)

  2. Open your browser and enter the IP address 127.0.0.2 (the one assigned in the virtual host setup)

Your site should now be accessible with PHP capabilities.

UPDATE

In response to a recent comment, here's a guide for sending emails via localhost.

Use a PHP library to send emails from your domain provider's email address:

  1. Download PHPMailer
  2. Extract the contents of the ZIP archive into a separate directory within BootstrapLandinPage, e.g., lib/PHPMailer
  3. Add the following lines at the beginning of your process.php file

    require 'path/to/PHPMailer/src/Exception.php';

    require 'path/to/PHPMailer/src/PHPMailer.php';

    require 'path/to/PHPMailer/src/SMTP.php';

    1. Refer to the example snippet on the documentation page where you downloaded the package and adapt it to your requirements.

As an alternative for local mailing:

  1. Download hMailServer
  2. Install it following their tutorial
  3. Navigate to the
    C:\Windows\System32\drivers\etc\hosts
    file and open it as an administrator
  4. Add the following line:
    127.0.0.1       localhost localhost.com
  5. Add the user [email protected] to your hMailServer
  6. Configure the mail account in your email client using:

    Username: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9bca1b8b4a9b5bc99b5b6bab8b5b1b6aaadf7bab6b4">[email protected]</a>

    Password: your_password

    IMAP/SMTP Server: localhost

UPDATE 2

This is how your PHP code would look when using PHPMailer:

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

$error_msg = array();
$success_msg = array();
$data = '';

// To prevent warnings or errors from displaying, set display_errors to 0
ini_set('display_errors', 0);

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // Validation logic goes here

}

// A function to output JSON-formatted data
function outputJson($data) {
    header('Content-Type: application/json');
    echo json_encode($data);
    exit;
}
function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

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

What is the best way to utilize jQuery for deleting the final <li> within a <ul> element?

One of my web pages contains an unorganized list similar to this: <ul id="myList"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> </ul> How can I target the last ...

Choose a punctuation mark using jQuery

I have a clock from W3Schools that I am working on here: http://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_clock My goal is to make the ":" in the clock flicker, toggling between visible and hidden. I believe I need to select the ":" using jQuer ...

What is the correct way to implement fetch in a React/Redux/TS application?

Currently, I am developing an application using React, Redux, and TypeScript. I have encountered an issue with Promises and TypeScript. Can you assist me in type-defining functions/Promises? An API call returns a list of post IDs like [1, 2, ..., 1000]. I ...

Emphasize a specific line of text within a <div> with a highlighting effect

I'm looking to achieve a similar effect as demonstrated in this fiddle As per StackOverflow guidelines, I understand that when linking to jsfiddle.net, it's required to provide some code. Below is the main function from the mentioned link, but f ...

Menu icon in Next.js/React/Tailwind not triggering close action when clicked again, causing responsiveness issue

Hey there, I'm relatively new to working with Next.js and React. Right now, I'm tackling the challenge of creating a responsive navbar that toggles open and closed when clicking on the hamburger icon (and should also close when clicked outside th ...

"Modifying Code Aesthetics in WebStorm/IDEA: A Step-by-

Here is the code style I am currently using in my JavaScript project: var obj = { a: 1 , b: 2 , c: 3 } var arr = [ 'a1' , 'a2' , 'a3' ] const w = 1 , w2 = 2 , w3 = 3 The team claims they are following npm's co ...

Gathering information associated with a specific session and user identification

I'm having trouble retrieving specific data from the current session and the "bidder_id" field in tbl_bidder where the value of the "accept" field is Accepted. Instead, I am getting data for all users in that table which is not what I intended. Below ...

Using React to Filter cards according to the selected value from an Autocomplete feature

I'm currently developing a React application that utilizes Material-UI's Autocomplete component to filter cards based on selected car brands. These cards represent various models, and the goal is to update them when a user picks a brand from the ...

Struggling with design issues while implementing ajax

I'm currently utilizing JQuery and AJAX to handle the submission and processing of a form. Whenever the user clicks on the like button, the information is sent via AJAX to my like.php file for processing. The like.php file processes the request and ge ...

Saving resources with a promise in Angular

I am facing a challenge while trying to handle a promise from the angular $resource save function. According to the documentation, I should be able to access the raw $http promise by using the $promise property on the returned object. Here is an example: ...

How can I use a dropdown with checkbox to toggle the visibility of a specific div in React?

I have come across a relevant question, but I am struggling to apply it to multiple divs. I haven't found a solution that quite fits my needs. Show or hide element in React Currently, I am using the dropdown with checkboxes from MUI. I am seeking a ...

Is it possible to identify the beginning of a download using Selenium?

Currently, I am using Python and Selenium to download a large batch of files. To ensure that each file is successfully downloaded, I am implementing a basic time.sleep() function, but I want to enhance efficiency and guarantee the completion of each downlo ...

Using multiple MaterialUI components in a JavaScript page with React can pose challenges

Incorporating multiple MaterialUI Cards in my project, I encountered an issue where all the cards would expand or the select values would change simultaneously. Despite using unique key values and mapped components with distinct keys, the problem persisted ...

Displaying information on a chartJS by connecting to an API using axios in VueJS

Struggling with inputting data into a ChartJS line-chart instance. The chart only displays one point with the right data but an incorrect label (named 'label'): Check out the plot image The odd thing is, the extracted arrays appear to be accura ...

Issue with Laravel's hasManyThrough Relationship not functioning as expected

Imagine the scenario where three tables are present as described below: projects- id, name environments- id, project_id, environment_id deployments- id, commit_hash In this setup, project id and deployment id are stored in environments. Both envi ...

Tips for avoiding passing an empty string when using local storage

I am currently using local storage to transfer two form inputs from a form on page A to a form on page B. The process is working smoothly, but I have encountered an issue. When I navigate directly to page B or visit it without inputting any data on page A, ...

When attempting to access http://localhost:3000/traceur in Angular 2 with the angular-in-memory-web-api, a 404 (Not Found) error

Hello, I'm encountering an issue with angular-in-memory-web-api. I have attempted to use angular2-in-memory-web-api in SystemJS and other solutions, but without success. I am currently using the official quickstart template. Thank you for any assistan ...

"Sending the selected pass selector as a parameter to the dispatched action is causing a typing

When a selector changes its value, I want to trigger an action. To achieve this, I passed the selector with a subscription instead of passing an observable. selectedSchedulingsOnPopup$ = this.store.pipe(select(selectSchedulingsByBranch)); this.store.disp ...

Securing the connection between clients and servers through encryption

For my mobile client, I am using Xamarin, with node.js as my backend and MongoDB as the database. The main issue I am facing is how to securely store user data in the database. If I only do server-side encryption, there is a risk of hackers intercepting th ...

ajax-jquery request declined

I have a jquery-ajax function that is being called multiple times with different IP addresses each time. This function makes a call to an action in the mvc4 controller responsible for executing a ping and returning the results. After analyzing the request ...