Jump to content

Recommended Posts

I'm total noob with php and been trying to do some email sending from form. My code looks like this atm:

 

<?php 
$viesti = '
<div>
  <form method="post" style="margin: 0px;">
    <div style="float: left;">X:
  </div>
    <div style="float: left;">
      <input type="text" name="leveys" size="5">	
  </div>
    <div style="float: left;">Y:
  </div>
    <div style="float: left;">
  		<input type="text" name="korkeus" size="5">	
	</div>
	<input type="submit" name="button" id="button" value="Laske" />
</form>
</div>
' ; ?>
<?php 
if (isset($_POST["button"])) { 
$to = "[email protected]";
$subject = "Postin aihe";
$message = "<html><head>HTML otsikko</head><body><table><tr><td><h1>sähköpostin</h1> sisältö.</td></tr></table></body></html>";
$from = "[email protected]";
$headers = "from: $from" . "\r\n";
$headers .= "reply-to: $replyto" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
mail($to,$subject,$message,$headers) or die ("Failure");
print("Your message was sent");
echo $viesti;
} else {
echo $viesti;
} ?>

 

So there is a little html form which I echo but I'd like to put it on other file like form.php and echo it from the file. I'd like to separate the html from php code and then I could just edit the html file and not the php code if I want to change it.

 

How to do this?

Link to comment
https://forums.phpfreaks.com/topic/171914-echoing-form-template-from-file/
Share on other sites

Why can't you just separate it yourself?

<div>
  <form method="post" style="margin: 0px;" action="form.php">
    <div style="float: left;">X:
     </div>
    <div style="float: left;">
      <input type="text" name="leveys" size="5">   
     </div>
    <div style="float: left;">Y:
     </div>
    <div style="float: left;">
        <input type="text" name="korkeus" size="5">   
      </div>
      <input type="submit" name="button" id="button" value="Laske" />
   </form>
</div>

 

Note I added 'action=form.php' so it'll POST to form.php (ie. your php code) and you can access it through $_POST['value'] (such as Laske)

I tried to use that 'action' because it looked like it could do the job but I never got it to work. Or then I dont really understand how it is suppose to work.

 

Im trying to do a little module to Joomla(CMS) and I have module.php and tmpl/default.php files.

default.php file is the template for that module what is seen on the site. So atm my code sit on the default.php file and works fine but I also like to put that email form to different file (form.php).

 

module.php doesnt really do anything atm but tells that there is this default.php template file.

 

If I now make file form.php and put that html form there with action="form.php" and put in default.php echo $_POST['Laske']; it wont sent the mail and browser goes to page ..mysite/form.php.

 

Am I missing something or trying to do it wrong?

default.php will have the form, with action=form.php.

 

Form.php will have something like this:

if isset($_POST['Laske']) {
   mail(.....);
   echo "E-mail sent!";// or use header(); to redirect
}

'action=form.php': The action of the form, is to send POST data to form.php, if that makes more sense to you.

Hmm that didnt work either. Dont think it reads that form.php since nothing happens when I press that Laske button. Not even that echo message that mail was sent.

 

default.php looks like this now:

<?php // no direct access
defined( '_JEXEC' ) or die( 'Restricted access' ); ?>
<div>
  <form method="post" action="form.php" style="margin: 0px;">
    <div style="float: left;">X:
  </div>
    <div style="float: left;">
      <input type="text" name="leveys" size="5">	
  </div>
    <div style="float: left;">Y:
  </div>
    <div style="float: left;">
  		<input type="text" name="korkeus" size="5">	
	</div>
	<input type="submit" name="button" id="button" value="Laske" />
</form>
</div>

 

And form.php looks like this:

<?php 
if isset($_POST['Laske']) { 
$to = "[email protected]";
$subject = "Postin aihe";
$message = "<html><head>HTML otsikko</head><body><table><tr><td>sähköpostin sisältö.</td></tr></table></body></html>";
$from = "[email protected]";
$headers = "from: $from" . "\r\n";
$headers .= "reply-to: $replyto" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
mail($to,$subject,$message,$headers) or die ("Failure");
echo "Your message was sent lomakkeella";
} ?>

 

Aint this just like you proposed?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.