Php variable in contact form / message body

久未见 提交于 2019-12-12 02:02:31

问题


I have a contact form that is only passing over some of the form fields and i wondered if anyone can help as to why?

The html form is as follows:

            <form enctype="multipart/form-data" method="post" action="referral.php" onsubmit="return submitForm()">
      <fieldset>
          <div class="field">
            <input name="name" id="name" type="text" class="validate" placeholder="Your Name" />
            <span class="form-msg" id="name-error">Please add your name</span>
          </div>
            <div class="field here-for-next-until">
              <input name="email" id="email" type="text" class="validate" placeholder="Your E-mail" class="text-input validate" />
              <span class="form-msg" id="email-error">Please add your e-mail address</span>
            </div>
            <div class="field">
              <input name="phone" id="phone" type="text" placeholder="Your Phone Number" class="text-input validate" />
              <span id="phone-error" class="form-msg" >Please add your phone number</span>
            </div>                        
            <div class="area here-for-next-until">
              <textarea name="message" id="message" class="text-input validate" placeholder="Introduce Yourself"></textarea><br/>
              <span class="form-msg" id="email-error">Please add an introduction to yourself</span>
              <input type="text" id="cap_val" name="cap_val" class="code validate" placeholder="Enter Code From Right"/>
              <img src="bin/captcha.php" id="cap"/><img src="images/refresh.jpg" id="cap-refresh" onclick="change_captcha()"/><br/>
            <span id="captcha-error" class="form-msg" >Please add the above code</span>
            <div class="clear here-for-next-until"></div>
          </div>
    </div>

   <div class="flt-lt w420">
    <h2 class="subheader">Client</h2>
          <div class="field">
            <input name="referralsname" id="name" type="text" class="validate" placeholder="Name" />
            <span class="form-msg" id="name-error">Please add your referrals name</span>
          </div>
            <div class="field here-for-next-until">
              <input name="referralsemail" id="email" type="text" class="validate" placeholder="E-mail address" class="text-input validate" />
              <span class="form-msg" id="email-error">Please add your referrals e-mail address</span>
            </div>
            <div class="field">
              <input name="referralsphone" id="phone" type="text" placeholder="Phone Number" class="text-input validate" />
              <span id="phone-error" class="form-msg" >Please add your referrals phone number</span>
            </div>                        
            <div class="area here-for-next-until">
              <textarea name="referralsmessage" id="message" class="text-input validate" placeholder="Introduce Yourself"></textarea><br/>
              <span class="form-msg" id="email-error">Please add an introduction to yourself</span>
            <div class="clear here-for-next-until"></div>
                        <div id="submit-holder">
              <input class="submit" type="submit" value="Submit"/>
              <input id="reset" class="submit" type="reset" value="Reset"/>
            </div>
          </div>
      </fieldset>
    </form>

Basically this is two very similar contact forms side by side that share a submit button

When posted it goes to this:

$owner_email = 'myemail.co.uk';
  $subject = A message from your site visitor ' . $_POST["name"];

  $messageBody = "\nVisitor: " . $_POST["name"] . "\n";
  $messageBody .= "Email Address: " . $_POST['email'] . "\n";
  $messageBody .= "Phone Number: " . $_POST['phone'] . "\n";
  $messageBody .= "Message: " . $_POST['message'] . "\n";

  $messageBody = "\nVisitor: " . $_POST["referralsname"] . "\n";
  $messageBody .= "Email Address: " . $_POST['referralsemail'] . "\n";
  $messageBody .= "Phone Number: " . $_POST['referralsphone'] . "\n";
  $messageBody .= "Message: " . $_POST['referralsmessage'] . "\n";

However when I submit the form I onyl get one set of details (the first 4 $messageBody)

Any help anyone could offer would be greatly appreciated.

Thanks again

David


回答1:


Change:

$messageBody = "\nVisitor: " . $_POST["referralsname"] . "\n";

To:

$messageBody .= "\nVisitor: " . $_POST["referralsname"] . "\n";

(.= instead of =, you are re-assigning value to $messageBody instead of chaining it to $messageBody)




回答2:


if i had to guess, i would say it is because you are repeating the IDs. In HTML element IDs have to be unique. make all IDs match your "name" values. im referring to

<input name="phone" id="phone"....
<input name="referralsphone" id="phone"....

change all these cases to:

<input name="phone" id="phone"....
<input name="referralsphone" id="referralsphone"....

I would also suggest checking the contents of POST. this question shows how Echo an $_POST in PHP



来源:https://stackoverflow.com/questions/25364959/php-variable-in-contact-form-message-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!