问题
This is what I am trying to do here.
- I have a html form to collect data from users
- Displaying the output on a textarea after submitting the form
- Two buttons to process the textarea content :- one for downloading the content as text file second one for sending the textarea content as email using php
Here, the output textarea is wrapped with a div output-main
and i dont want to display the this div before the form submit.
Now My HTML code :
<div class="emcsaninfo-symcli-main">
<form id="main" name="main" action="#text" method="post" >
<div class="block-with-text-area">
<div class="input-quest-with-text-area">Device List</div>
<div class="input-resp-with-text-area"><span><textarea class="textarea" name="devs" ></textarea></span></div>
</div>
<div id="devseq" >
<div class="block">
<div class="input-quest">Starting Device</div>
<div class="input-resp"><span><input class="textbox" id="sdev" name="sdev" type="text" size="5" maxlength="5" value="<?php if(isset($_POST['sdev'])) { echo htmlentities ($_POST['sdev']); }?>" /></span> </div>
</div>
</div>
<div id="metamemcnt" >
<div class="block">
<div class="input-quest">Meta member count(Excluding Meta Head)</div>
<div class="input-resp"><span><input class="textbox" id="mem_count" name="mem_count" type="text" size="5" maxlength="3" value="<?php if(isset($_POST['mem_count'])) { echo htmlentities ($_POST['mem_count']); }?>" /></span></div>
</div>
</div>
<div id="metacnt" >
<div class="block">
<div class="input-quest">How many Meta Devices</div>
<div class="input-resp"><span><input class="textbox" id="meta_count" name="meta_count" type="text" size="5" maxlength="3" value="<?php if(isset($_POST['meta_count'])) { echo htmlentities ($_POST['meta_count']); }?>" /></span></div>
</div>
</div>
<div class="submit-clear">
<input id="generate" type="submit" name="script" value="create my cli script" />
<input type="submit" id="clear" name="clear" value="clear" />
</div>
</form>
</div>
Since I dont want to display the output area prior to form submit and I am first checking whether user entered data in the form field, and if yes creating the output div
PHP code below
<?php
if((!empty($_POST['sdev']) && !empty($_POST['mem_count']) && !empty($_POST['meta_count'])) || !empty($_POST['devs']) ){
?>
<div id="output-main">
<?php
echo '<form action=" " method="post">';
echo '<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly>';
//other PHP Codes here
echo '</textarea>';
?>
</div>
<?php
echo '<input type="submit" id="download" value="submit" name="submit"></input>';
echo '<input class="textbox" type="text" id="email-box" name="email-box" value="Enter Your Email ID here">';
echo '<input type="submit" id="download1" value="submit2" name="submit2"></input>';
if($_POST['submit2'] == 'submit2') {
$to = $_POST['email-box'] ;
$message = $_REQUEST['output_textarea'];
$subject = 'form meta cli script';
$headers = "From: info@domain.com\r\n";
$headers .= "Reply-To: me@domain.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers);
echo 'message sent successfully';
}
}
?>
Issue that I am facing:
I am able to download the content as txt file but when I a trying to send the content via email, it is not working, it just clear the output div
section.
But If I remove the the first PHP condition ie,
<?php
if((!empty($_POST['sdev']) && !empty($_POST['mem_count']) && !empty($_POST['meta_count'])) || !empty($_POST['devs']) ){
?>
Both download and email function working perfectly. But that time, I can not hide the output div
section prior to form submit.
I am not understanding whats going wrong here.
I have added
value="<?php if(isset($_POST['sdev'])) { echo htmlentities ($_POST['sdev']); }?>"
to keep the user entered data on its field after form submission. BUt when I am clicking email button, it clearing these fields (download button working fine and it keeps data in field after hitting downloading buttons also)
来源:https://stackoverflow.com/questions/18361291/php-form-submission-issue