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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:48:13+00:00 2026-05-28T20:48:13+00:00

I’ve never done that before and simply need a little advice how to do

  • 0

I’ve never done that before and simply need a little advice how to do so …

I have a index.php file with a simple contact form.

<form id="contactform" method="post" action="<?php echo $_SERVER["SCRIPT_NAME"] ?>">

The index.php file has the following script on top.

<!DOCTYPE html>
<html dir="ltr" lang="en-US">

<?php

    //Vars
    $Name = Trim(stripslashes($_POST['author']));
    $EmailFrom = Trim(stripslashes($_POST['email']));
    $Subject = Trim(stripslashes($_POST['subject']));
    $Type = Trim(stripslashes($_POST['type']));
    $Comment = Trim(stripslashes($_POST['message']));
    $EmailTo = "address@something.com";

    //Validation
    $valid = true;
    if ( $Name == "" ) $valid = false;
    if ( isValidEmail( $EmailFrom ) == 0 ) $valid = false;
    if ($Subject == "") $valid = false;
    if ($Comment == "") $valid = false;
    function isValidEmail( $email = null ) {
        return preg_match( "/^[\d\w\/+!=#|$?%{^&}*`'~-][\d\w\/\.+!=#|$?%{^&}*`'~-]*@[A-Z0-9][A-Z0-9.-]{1,61}[A-Z0-9]\.[A-Z]{2,6}$/ix", $email );
    }

    //Body
    $Body = $Type;
    $Body .= "\n\n";
    $Body .= $Comment;

    //Headers
    $email_header = "From: " . $EmailFrom . "\r\n";
    $email_header .= "Content-Type: text/plain; charset=UTF-8\r\n";
    $email_header .= "Reply-To: " . $EmailFrom . " \r\n";

    //Send
    if ($valid)
        $success = mail($EmailTo, $Subject, $Body, $email_header);

?>

I have two questions now:

1.)
How exactly can I render/not-render certain stuff when either the validation went wrong or a success or an error comes back when submitting the mail?

e.g. I know that I can do that!

        if ( !$valid )
            print "Failed to make contact. Enter valid login credentials! <a href='/#contact' title='try again'>try again?</a>";

        if ( $success )
            print "Successfully made contact.";
        else
            print "Failed to make contact. <a href='/#contact' title='try again'>try again?</a>"; */

    ?>

However $valid will always be wrong on page-load when not submitting the form and also the email will always return the error message on the first page load. How can I only render or not render specific stuff when the form is submitted?

E.g. When submitting the form and a success comes back I don’t want to render the #contactform anymore. I simply want to print “Successfully made contact” into an h1 or so.

How can I make that happen? It’s probably rather simple I just can’t find a solution for myself.

2.)
When using $_SERVER["SCRIPT_NAME"] or PHP_SELF as action the url after submitting the form will always change to “mydomain.com/index.php”. Can I prevent that from happening? I want to submit the index.php file itself however I just don’t like it when /index.php is written into the url. Is it possible to stop that from happening?

Thank you for your help!

  • 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-28T20:48:14+00:00Added an answer on May 28, 2026 at 8:48 pm

    Matt,

    For the first question as to printing to the screen based on success or failure of the email, your checks seem fine, but you probably aren’t going to get an email failure in time to display that to the screen. That said, you just need to wrap your second set of code in an if statement. Something like this:

    if( isset($_POST['Submit']) ){  //only attempt to display if form submitted.
        //Your code here
    }
    

    As for not including the directory in the form action, there are many ways to do this, but here’s one:

    $scriptString= explode('/',$_SERVER['SCRIPT_NAME']);
    $scriptSize = count($scriptString)-1;
    $script = $scriptString[$scriptSize];
    

    And then use $script in the form action.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I need a function that will clean a strings' special characters. I do NOT

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.