0

I wrote a simple php script for a form, tested it out, and worked as expected. But when I actually added the script into the original project that I am working on, it suddenly stopped working? I am sure it has nothing to do with the php script as for it worked properly when I tested it; so basically what I am thinking about is that I probably wrote the action attribute wrong? I am pretty sure it is a rookie mistake. Eventually, I am really new to php.

Regards.

HTML code:

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
               <h4 class="modal-title" id="exampleModalLabel">New message</h4>
            </div>

            <div class="modal-body">
               <form action="contact.html" method="post">
                <div class="form-group">
                  <label name="email" class="control-label">Email:</label>
                  <input type="text" class="form-control" id="recipient-name">
                </div>
                <div class="form-group">
                    <label name="phone" class="control-label">Phone:</label>
                        <input type="text" class="form-control" id="recipient-mobile">
                </div>
                <div class="form-group">
                   <label name="message" class="control-label">Message:</label>
                   <textarea class="form-control img-responsive" rows="5" id="messageText"></textarea>
                </div>
               </form>
            </div>

            <div class="modal-footer">
               <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
               <button type="button" id="resetText" class="btn btn-default">Reset</button>
               <input type="button" value="Send message" name="send" class="btn btn-danger colorbg"/>
              </div>
        </div>
    </div>
</div>

PHP Code:

<?php
if(isset($_POST['send'])){

    $to = 'domain@mail.com';
    $subject = 'Solutions';
    $mail = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $mailHeader = "From: $mail \r\n Phone: $phone";
    $formcontent="Message: $message";

    if (!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
        echo "<script language='javascript'>
                alert('E-mail address is not valid');
                var email = document.getElementById('recipient-name');
                email.className += ' border-red';
              </script>";
    } else {
      echo "<script language='javascript'>
              var email = document.getElementById('recipient-name');
              email.className = '';
              email.className += ' form-control';
            </script>";

      if (mail($to , $subject, $formcontent, $mailHeader)) {

        echo "<script>
        window.setTimeout(function () {
            window.location.href = 'test.html';
        }, 3000);
        </script>";

      } else {
        echo "<script language='javascript'>alert('There was an error. Please try again.')</script>";
      }
  }
}
?>

Please note that I uploaded the project on my website in order to actually test the script so the link is something like this: website.com/project/index.html. And I changed the action to action="script/contact.php", action="./script/contact.php, action="contact.php" none worked.

Ray Joe
  • 182
  • 1
  • 2
  • 11

3 Answers3

1

There are two things that makes this not work.

  1. The "submit" button (or any of the other buttons for that matter) is not inside the form-tags. They can be outside, if you assign an ID to the form and assign inputs outside the form, to that form.
  2. There isn't actually a submit-button. You have a regular button. It should be of type="submit", not type="button" (and type="reset" for reset buttons).

In HTML5, you can assign inputs to a form, even outside the actual form-tags. You can do that by assigning an ID to the form (in this example, "myform") and then specifying the form-attribute on your input, like this.

<form id="myform" method="get" action="something.php">
    <input type="text" name="name" />
</form>
<input type="submit" form="myform" />

You also, as the other answer already pointed out, the action targets a .html file, which under normal configurations would not parse PHP, but display it as text instead.

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • `` that's outside `
    `. won't work.
    – Funk Forty Niner Dec 06 '15 at 20:46
  • @Fred-ii- In HTML5, you can assign ids to forms, and have input values outside the form, that was exactly the point I was trying to make :-) – Qirel Dec 06 '15 at 20:47
  • I don't get why you're suggesting a GET method here `method="get"` since the OP's using POST arrays. – Funk Forty Niner Dec 06 '15 at 20:47
  • It's pseudo-code, to show as an example - that you can in fact have inputs *outside* the actual form tags. It isn't replicating his exact issue. – Qirel Dec 06 '15 at 20:49
  • @Qirel, it worked but now every email I type I get the "not a valid email" error. – Ray Joe Dec 06 '15 at 21:57
  • @RayJoe Alright, from the glance of it, seems like your mail-header is off, it doesn't have a "phone" attribute. You might want to read [PHP mail form doesn't complete sending e-mail](http://stackoverflow.com/questions/24644436/php-mail-form-doesnt-complete-sending-e-mail). – Qirel Dec 06 '15 at 22:13
0

I don't see any submit. Or is that handled by JavaScript somewhere?

Also, the form action= is a html file, if you want the php to work in there, you'll need a .php file.

There is nothing being posted in the html you're showing that is named "send". ( if(isset($_POST['send'])) )

Santy
  • 387
  • 2
  • 7
0

I found the solution here for a similar problem. I hope this is the right place for my answer.

I use buttons A-Z to filter last names in 'listContacts.php'. Each button triggers a submit. The submit was working from the beginning on

var $char='';
var $characterfilter=function charfilter($char){;
  $('#coll').prop('value', $char);
  var val2=$('#coll').val();
  $('#listcontacts').submit();
};
$('#a').click(function(){
  $characterfilter('a');
});

The problem:

<form id='listcontacts' href='' title='use the tabulator to move in the form' style='position: relative; overflow:hidden; height:25em; width:95%' method='post' accept-charset='UTF-8' action='admin.php?listContacts'>

A switch-function in 'admin.php' as below controls the actions:

switch ($action){
  case 'login':
    login();
    break;
  case 'logout':
    logout();
    break;
  case 'editService':
    editService();
    break;
  case 'deleteService':
    deleteService();
    break;
  case 'listContacts':
    listContacts();
    break;
  case 'newContact':
    newContact();
    break;
  case 'editContact':
    editContact();
    break;
  case 'deleteContact':
    deleteContact();
    break;
  default:
    listServices();
}

When I submitted the form, the Switch in 'admin.php' always returned the default, so I saw the form 'listServices.php' instead after submitting 'listContacts' by using one of the filterbuttons.

The reason for this flaw was: the first call to 'listContacts.php' had been executed by a command:

<a href='admin.php?action=listContacts' >List Contacts</a>

so 'action' had already been set to 'listContacts' After removing 'action=...' from the formcall as below the form was working.

<form id='listcontacts' href='' title='use the tabulator to move in the form' style='position: relative; overflow:hidden; height:25em; width:95%' method='post' accept-charset='UTF-8'>

It was a little puzzle, took me a few hours and I hope I can help others saving time by posting this.

MikeOffenbach
  • 109
  • 1
  • 5