I have a mail form on my HTML that calls a PHP. When I click Submit, It redirects to the PHP and shows various alerts about the form.
I want it not to redirect to the PHP and show alerts on HTML. I tried to redirect back from PHP but in that case it didn't show any alerts.
I couldn't manage to do that. I've read may similar posts but they didn't help.
Can anyone help?
HTML Form:
<form method="post" action="sendmail.php">
<input name="id" type="hidden" value="1775">
<table cellpadding="10px" border="0">
<tr>
<td align="right"><iletisimmetni>ad soyad :</iletisimmetni></td>
<td><input class="yuvarlak" name="sender_name" type="text"></td>
</tr>
<tr>
<td align="right"><iletisimmetni>e-posta :</iletisimmetni></td>
<td><input class="yuvarlak" name="sender_email" type="email"></td>
</tr>
<tr>
<td align="right"><iletisimmetni>konu :</iletisimmetni></td>
<td><input class="yuvarlak" name="subject" type="text"></td>
</tr>
<tr>
<td align="right" valign="top"><iletisimmetni>mesaj :</iletisimmetni></td>
<td><textarea class="yuvarlak" rows="10" cols="40" name="message"
onkeyup="textLimit(this, 250);"></textarea></td>
</tr>
<tr>
<td colspan="2" align="right"><input class="urunbutton" value="Gönder" name="send"
type="submit" id="send"></td>
</tr>
</table>
</form>
Here is the PHP:
<?php
function spamcheck($field)
{
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
if (isset($_REQUEST['sender_email']) && !empty($_REQUEST['sender_name']) &&
!empty($_REQUEST['subject']) && !empty($_REQUEST['message']))
{
$mailcheck = spamcheck($_REQUEST['sender_email']);
if ($mailcheck==FALSE)
{
echo "<script>alert('Lütfen geçerli bir e-posta adresi girin.')</script>";
// $URL="http://pantuff.com/testing/iletisim.html";
// header ("Location: $URL");
}
else
{//send email
$sender_name = $_REQUEST['sender_name'] ;
$sender_email = $_REQUEST['sender_email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$headers = "From: ".$sender_name." <".$sender_email.">
";
$headers .= "MIME-Version: 1.0
";
$headers .= "Content-Type: text/html; charset=utf-8
";
mail("info@pantuff.com", "$subject", "$message" . " " . "$sender_name", $headers);
echo "<script>alert('Mesajınız alınmıştır. Teşekkür ederiz.')</script>";
// $URL="http://pantuff.com/testing/iletisim.html";
// header ("Location: $URL");
}
}
else
{
echo "<script>alert('Lütfen tüm alanları doldurun.')</script>";
// $URL="http://pantuff.com/testing/iletisim.html";
// header ("Location: $URL");
}
?>
Thanks.
I think what you're trying to do here (and what is common practice) is to embed your HTML code into the PHP file, instead of having 2 separate files.
It is perfectly valid to have HTML code outside of <?php ?>
tags.
For example you would have a single file sendmail.php
look something like this:
<?php
if(isset($_POST['id']))
{
// checks and alerts
// ...
}
?>
<form method="post" action="sendmail.php">
<input name="id" type="hidden" value="1775">
<table cellpadding="10px" border="0">
<!-- ... -->
</form>
And your client would access the from via the URL to the sendmail.php.
It looks like you are trying to validate the form prior to sending it. To do this on the client side prior to actually submitting the form, you can use javascript. jQuery has a plugin that might save you some time:
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
change your code as
<form method="post" action="">
and then write the php code in the same html page.
For validation of form use Javascript code on the same page.
You can combine it into one file example index.php
<?php
//php code in here
?>
<form action="index.php">
<input type=text />
<!-- html code in here -->
</form>
when we access php file, php will be executed by server. HTML and javascript will be read by browser. when you redirect, the script is never read by browser because the server give browser another page.