-1

PHP を使用するのはこれが初めてで、すべてが 1 つのページで機能するフォームをつなぎ合わせました。問題は、最初にページにアクセスしたときに「連絡できるようにメールアドレスを入力してください」と表示されますが、送信ボタンが押された後にエラー/成功メッセージのみが必要です。

さらに、出力時にすべてのメッセージを div でラップしたいと考えています。

次のようなものを使用することを提案する投稿を読みましたが、実装しようとすると、送信を押してもメッセージが表示されません:

if (isset($_POST['submit'])) 
{ 
    // Process Form
}
else
{
    // Show Form
}

とにかく、ここに私の完全なコードがあります:

<?php 
 $to = "me@gmail.com" ; 
 $from = $_REQUEST['Email'] ; 
 $name = $_REQUEST['Name'] ; 
 $headers = "From: $from"; 
 $subject = "Web Contact Data";
 $startmonth = $_REQUEST['StartMonth'];
 $startyear = $_REQUEST['StartYear'];
 $endmonth = $_REQUEST['EndMonth'];
 $endyear = $_REQUEST['EndYear'];
 $message = $_REQUEST['Message'];

 $fields = array(); 
 $fields{"Name"} = "Name"; 
 $fields{"Email"} = "Email"; 
 $fields{"Phone"} = "Phone"; 

 $selectedProjects  = 'None';
if(isset($_POST['projects']) && is_array($_POST['projects']) && count($_POST['projects']) > 0){
    $selectedProjects = implode(', ', $_POST['projects']);
}
 $selectedSkills  = 'None';
if(isset($_POST['skills']) && is_array($_POST['skills']) && count($_POST['skills']) > 0){
    $selectedSkills = implode(', ', $_POST['skills']);
}
$selectedNoRush  = 'None';
if(isset($_POST['norush']) && is_array($_POST['norush']) && count($_POST['norush']) > 0){
    $NoRush= implode(', ', $_POST['norush']);
}
$selectedWhenReady  = 'None';
if(isset($_POST['whenready']) && is_array($_POST['whenready']) && count($_POST['whenready']) > 0){
    $WhenReady= implode(', ', $_POST['whenready']);
}
$selectedBudget  = 'None';
if(isset($_POST['budget']) && is_array($_POST['budget']) && count($_POST['budget']) > 0){
    $selectedBudget= implode(', ', $_POST['budget']);
} 

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);}
$body .= "\n" . 'Selected Projects: ' . $selectedProjects . "\n";
$body .= 'Selected Skills: ' . $selectedSkills . "\n\n";
$body .= 'Start Date: ' . $startmonth . " " . $startyear . " " . $NoRush . "\n";
$body .= 'End Date: ' . $endmonth . " " . $endyear . " " . $WhenReady . "\n";
$body .= 'Budget: ' . $selectedBudget . "\n\n";
$body .= 'Message:' . $message . "\n";



 $headers2 = "From: me@gmail.com"; 
 $subject2 = "Thank you for contacting us"; 
 $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usually within 48 hours.";


 if($from == '') {print "Please enter your email address so we can contact you.";} 
 else { 
 if($name == '') {print "Please enter your name so we know who sent this.";} 
 else {
 if($message == '') {print "Please give a brief description so we can understand how best to help.";} 
 else {
 $send = mail($to, $subject, $body, $headers); 
 $send2 = mail($from, $subject2, $autoreply, $headers2); 
 if($send) 
 {print "Thank you. Your request has been successfully submitted.";} 
 else 
 {print "We encountered an error sending your mail, please check your details are correct or email us at hello@lyonempire.co.uk"; } 
}}}
 ?>

ありがとう。

PS。ブラケットとコードのレイアウトのより良い方法を誰かが指摘したい場合は、お気軽に。もっとうまくやれることはわかっていますが、どうすればよいかわかりません。

MC

更新 - HTML は次のとおりです。

<form method="post">

 <h3>Your Details</h3>

<div class="formrow">
    <input type="text" name="Name" maxlength="99" id="fullname" placeholder="Name" />
    <input type="email" name="Email" maxlength="99"placeholder="Email Address" />
    <input type="tel" name="Phone" maxlength="25" placeholder="Phone Number" />
</div>

<h3>Project Type</h3>

<div class="formrow">
    <fieldset>
        <input type="checkbox" id="type1" name="projects[]" value="Web">
       <label for="type1">Web</label>
        <input type="checkbox" id="type2" name="projects[]" value="Digital Media">
       <label for="type2">Digital Media</label>
        <input type="checkbox" id="type3" name="projects[]" value="Consultancy">
       <label for="type3">Consultancy</label> 
    </fieldset> 
</div>

<h3>A Brief Description</h3>

<div class="formrow">
    <textarea name="Message"></textarea>
</div>

<h3>What do you do?</h3>

<div class="formrow">
    <fieldset>
        <div class="skillrow">
            <input type="checkbox" id="skill1" name="skills[]" value="Website Design">
               <label for="skill1">Website Design</label>
            <input type="checkbox" id="skill2" name="skills[]" value="Social Networking">
               <label for="skill2">Social Networking</label>
            <input type="checkbox" id="skill3" name="skills[]" value="E-Commerce">
               <label for="skill3">E-Commerce</label>
        </div>

        <div class="skillrow">
            <input type="checkbox" id="skill4" name="skills[]" value="Content Management Systems">
               <label for="skill4">Content Management Systems</label>
            <input type="checkbox" id="skill5" name="skills[]" value="Video">
               <label for="skill5">Video</label>
            <input type="checkbox" id="skill6" name="skills[]" value="Images">
               <label for="skill6">Images</label>
        </div>

        <div class="skillrow">
            <input type="checkbox" id="skill7" name="skills[]" value="Social Media">
               <label for="skill7">Social Media</label>
            <input type="checkbox" id="skill8" name="skills[]" value="Strategy">
               <label for="skill8">Strategy</label>
            <input type="checkbox" id="skill9" name="skills[]" value="Best Practice">
               <label for="skill9">Best Practice</label>
        </div>

        <div class="skillrow">
            <input type="checkbox" id="skill10" name="skills[]" value="Not Sure">
               <label for="skill10">Not Sure</label> 
        </div>
    </fieldset>
</div>

<h3>Ideal Start Date</h3>

<div class="formrow">
    <label class="drops">
        <select name="StartMonth">
            <option value="">---</option>
            <option value="January">January</option>
            <option value="February">February</option>
            <option value="March">March</option>
            <option value="April">April</option>
            <option value="May">May</option>
            <option value="June">June</option>
            <option value="July">July</option>
            <option value="August">August</option>
            <option value="September">September</option>
            <option value="October">October</option>
            <option value="November">November</option>
            <option value="December">December</option>
        </select>
    </label>
    <label class="drops">
        <select name="StartYear">
            <option value="">---</option>
            <option value="2013">2013</option>
            <option value="2014">2014</option>
            <option value="2015">2015</option>
        </select>
    </label>
    <input type="checkbox" id="norush" name="norush[]" value="No Great Rush">
           <label for="norush">No Great Rush</label>
</div>

<h3>Ideal End Date</h3>

<div class="formrow">
    <label class="drops">
        <select name="EndMonth">
            <option value="">---</option>
            <option value="January">January</option>
            <option value="February">February</option>
            <option value="March">March</option>
            <option value="April">April</option>
            <option value="May">May</option>
            <option value="June">June</option>
            <option value="July">July</option>
            <option value="August">August</option>
            <option value="September">September</option>
            <option value="October">October</option>
            <option value="November">November</option>
            <option value="December">December</option>
        </select>
    </label>
    <label class="drops">
        <select name="EndYear">
            <option value="">---</option>
            <option value="2013">2013</option>
            <option value="2014">2014</option>
            <option value="2015">2015</option>
        </select>
    </label>
    <input type="checkbox" id="whenready1" name="whenready[]" value="When It's Ready">
           <label for="whenready1">When it's good and ready</label>
</div>

<h3>Estimated Budget</h3>

<div class="formrow">
<input type="radio" id="budget1" name="budget[]" value="Under 800">
   <label for="budget1">Under &#163;800</label>
<input type="radio" id="budget2" name="budget[]"value="800 to 1600">
   <label for="budget2">&#163;800 - &#163;1600</label>
<input type="radio" id="budget3" name="budget[]" value="Over 1600">
   <label for="budget3">Over &#163;1600</label> 
</div>

<h3>Good to Go?</h3>

<div class="goodtogo">
    <input type="submit" name="send" value="Submit Details Now!">
</div>
</form>
4

1 に答える 1

0

編集: コメント セクションで述べたように、この最初の部分は正しくありません。間違った回答をして申し訳ありません。action 属性が html 5 でオプションであることを知りませんでした。投稿する前に調査を行うべきでした。謝罪いたします。


これは正しくありません:

フォームが処理される php ファイルを指定していないため、何も表示されません。フォーム タグを次のように編集します (index.php を php ファイル名に置き換えます)。

<form action="index.php" method="post">

また、「送信」部分

isset($_POST['submit'])

送信ボタンの名前と一致する必要があります:

<input type="submit" name="send" value="Submit Details Now!">

次のようにする必要があります。

<input type="submit" name="submit" value="Submit Details Now!">
于 2013-06-26T14:07:16.130 に答える