-1

I have made a website called coodos.co, I am a newbie at php whenever someone submits the contact us form the php page opens and never closes, what I want to do is that the php code executes in the backend instead of loading a page called contact.php in front of the user,

here is my php code

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $msg = $_POST['msg'];   

    $to='info@coodos.co'; // Receiver Email ID, Replace with your email ID
    $subject='Form Submission';
    $message="Name :".$name."\n"."email:".$email."\n"."Wrote the following :"."\n\n".$msg;
    $headers="From: ".$email;
    if(mail($to, $subject, $message, $headers)){
        exit();
    }

?>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
JohnSmith
  • 113
  • 5
  • Hi John, your code does execute in the "backend", i.e. all PHP code is executed on the server and not on the client's machine. The processing you have in your snippet there calls `exit()` which means that the user will see a blank page. How about instead of doing that you could redirect them using `header('Location: '.$newURL);` before `exit();` where `$newURL` would be some HTML page that says something like "Thank you for your mail"? – mickadoo Apr 05 '20 at 13:45

2 Answers2

2

this has nothing to do with the way php works but with the way forms in HTML work.
I assume that somewhere in your page you have the line

<form action="/contact.php">

or something similar. The action link will tell the Browser where to go after the user submits the Form. To supress this you can either use JavaScript as described here or make it just link back to the frontpage.
Alternatively you could also redirect from your contact page to your frontpage by sending a redirect header like this:

header('Location: '.$newURL);
seven_seas
  • 703
  • 4
  • 21
  • @JohnSmith No problem, happy to help :) If you could press the tick to accept the answer I'd greatly appreciate it – seven_seas Apr 05 '20 at 14:04
0

If I understood you correctly you want to execute some external php-file and not leave the page you are on when submitting for instance a form.
Hope this is correct.

Option 1 - header()

One often used way to achieve a very similar behaviour is the header method. This redirects you to a specific file.

header('location: index.php');

This may also be used to pass additional data in the URL as $_GET values.
Small example:

    header('location: index.php?login=success');

Note: You still want to keep the exit() to prevent code after the redirection from being executed.

Option 2 - PHP_SELF

A little bit more tricky but also a thing you can use is redirecting to the page you are already on. For this instead of a hardcoded file path you can use $_SERVER['PHP_SELF'].

Handle this witch care!
A user has ways to manipulate the url which means you have to cover potential cross site scripting (XSS) attacks. This easily can be done with htmlspecialchars, but is very important!

Here is how it can be used inside a form:

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">

Because you are redirecting on the page you are one, you very likely want to have another section of PHP code to check for either $_GET or $_POST data which may be sent.

Phy
  • 248
  • 1
  • 8