0

I am using Php for the first time and trying to make a contact us form. I have made the form with inline php inside html code and saved the file as .php and it worked. Now that looked really ugly and my nodejs server doesn't serve my php files so I have tried to take out the php code into a separate file.

So now I have two files -

  1. index.html which has form
  2. mail.php

index.html

 <h2>PHP FORM </h2>
 <form action="mail.php" method="POST">
    <label for="user">Name</label>
     <input type="text" id="user" name="users-name"><br>
     <label for="email">Email</label>
      <input type="email" id="email" name="users-email"><br>
      <label for="cars">Category</label>
       <select name="users-cat" id="cars">
           <option value="volvo">Volvo</option>
           <option value="saab">Saab</option>
           <option value="mercedes">Mercedes</option>
           <option value="audi">Audi</option>
       </select><br>
     <label for="subject">Subject</label>
     <input type="text" id="subject" name="users-subject"><br>
     <label for="msg">Message</label>
      <input type="text" id="msg" name="users-message"><br>
      <input type="submit">
 </form>

mail.php is like this -

    <?php 
        if (isset($_POST['users-name']) ||
                isset($_POST['users-email']) ||
                isset($_POST['users-subject'])) { 
            $admin_email = "username@gmail.com"; 
            $name = stripslashes($_POST['users-name']);
            $subject = stripslashes($_POST['users-subject']);
            $email = stripslashes($_POST['users-email']);
            $category = stripslashes($_POST['users-cat']);
            $message = stripslashes($_POST['users-message']); 
            //send email
            $maiL_status = mail($admin_email, "$subject", "Contact Email: " . $email . "\n" . "Name: " . $name . "\n" . "Category: " . $category . "\n" . "Message: " . $message);
     
            //Email response
            echo '<h4 class="text-center">THANK YOU.</h4>';
            echo '<h6 class="text-center">Your message is now being processed.</h6>';
            echo '<h6 class="text-center">We will get back to you promptly.</h6>';
            header("Location: index.html?message=ThankYou");
        }else{
            echo '<h4 class="text-center">Problem</h4>';
        }
    ?>



So my question here is - how can I submit my form with from action using mail.php and redirect back to html page with a message. I am open use Jquery or javascript. I wanna send back the echo response to html page and show message in html page.

I have gone through a lot of stackvoerflow post but I couldn't figure out

  1. How do I make a redirect in PHP?
  2. PHP Pass Data with Redirect

Edit:

I have solved this with javascript using -

let msg = window.location.href;
        msg = msg.split('=')[1];
        console.log(msg);
        if(msg!=null){
            $('#form_id').hide();
            $('#repos').show();
            $('#repos').text(msg);
        } 
Greyfrog
  • 926
  • 1
  • 8
  • 19
  • 1
    You should look in to "AJAX" -- you use javascript in the background to post the form to the PHP script, and get the result in javascript, then use javascript to display the result, along with possibly navigating the user on to a new page after they've seen your result message. That is a far more modern way of doing it. Another alternative is to redirect the user with a query param like `index.php?showWelcomeMessage=1` and conditionally display your message if that query param is present. – Chris Baker Oct 30 '20 at 01:37

2 Answers2

0

I wrote a structure of program that you can refer to. Part of the code is omitted.

<?php
// Process anything about sending mail.
if (isset($_POST['users-name'])) {
    // Some codes are omitted
    $msg = "";
    $hasError = true;
    if ($hasError) {
        $msg = "xxx";
    }
    $redirectUrl = "/xxx/contact.php?msg={msg}";
    header("Location: {$redirectUrl}");
}
?>
<html>
<script type="text/javascript">
// Process anything about show msg.
<?php
if (isset($_GET['msg']) && $_GET['msg']) {
    $msg = $_GET['msg'];
    // output html for init js var msg
    echo "var msg = \"{$msg}\";";
}
?>

// js code: alert the msg if msg is not empty.
// Some codes are omitted
// other way: show the msg by html element.

</script>

<form action="/xxx/contact.php" method="POST">
<!-- form content -->
</form>
</html>
Xingchao
  • 96
  • 4
  • So if I understand correctly from you code - (1) php code is inside the contact.php file and html code is inside the index.html file? Right? (2) I tried to do that but I get an error in html file - "Uncaught SyntaxError: Unexpected token '<" – Greyfrog Oct 29 '20 at 21:55
  • 1
    This will work, but structurally isn't the cleanest. This is like PHP 101 type of code, it will work, but if your project is big or you are getting paid to write this code, this is definitely not how you want to do it. If this is conceptually new to you, I highly, highly suggest seeking out some PHP tutorials. – Chris Baker Oct 30 '20 at 01:40
  • 1
    @ChrisBaker I agree with you. Thanks. – Xingchao Oct 30 '20 at 02:10
  • 1
    @Greyfrog If you do not use php framework, I suggest you read articles, https://code.tutsplus.com/tutorials/how-to-use-ajax-in-php-and-jquery-cms-32494, https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php. – Xingchao Oct 30 '20 at 02:17
  • @Xingchao the link is broken but nayway. I was trying to get the message back in my html file, not in php file. So I was trying the trick where I can get the response from the url using window location. Anyway this is a small project and just required this form. I usually do stuff with nodej, javascript and firebase but just for this small task I don't want to write any firebase function. Basically I am trying to avoid any server side coding for now. – Greyfrog Oct 30 '20 at 19:14
-1

you have solved it alright. But, I think, the task can be simplified this way by putting this javascript outside the closing php tag in your php file, that is at the end

<script language="javascript">location.href="index.html?msg="<?php echo $msg ?>";</script>
Padhoo
  • 1
  • 3
  • I wanted to receive the message in the html file, not in the php file. The code I have put up there is inside the html file. Putting php code inside the .html file would not get executed, as mentioned above. So in a nutshell, I was trying to get the message back in my html file, not in php file but thanks for the input. – Greyfrog Oct 30 '20 at 19:10