1

i have code for valid some form and it's working fine

my code jop that is valid form then send message in the next page this is the full code

    <?
                    $name=$_POST['name'];
                    $email=$_POST['email'];
                    $msg=$_POST['msg'];

                    if($_POST['submit']){

                        if(empty($name))

                            $errorname = ("<span id='error'>no name</span>");



                        if(empty($email))

                            $erroremail = ("<span id='error'>no email</span>");



                        if (empty($msg))

                            $errormsg = ("<span id='error'>no msg</span>");




                            else {

                    echo'<script>window.location = "process.php";</script>';

                                }
                        }


?>


<form dir="rtl" action="" method="post">
                            <div>name<span class="nothing">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><input type="text" name="name" id="name" dir="rtl"> 


            <span id="errororg">&nbsp;<?= $errorname; ?></span></div>
                            <div >email<span class="nothing">&nbsp;&nbsp;</span><input  type="email" color="#fff" name="email" id="email"><span id="errororg">&nbsp;&nbsp;<?= $erroremail; ?></span></div>
                            <span>msg</span>
                            <div><textarea rows="10" name="msg" id="comment" dir="rtl"></textarea><span id="errororg"><?= $errormsg; ?></span></div>
                            <div><input type="submit" name="submit" value="send" dir="rtl"></div><br>
                        </form>

=======

so it's working fine and the valid message and every thing fine but the message arrived empty !

this is code of next page

<?

$name=$_POST['name'];

$email=$_POST['email'];

$msg=$_POST['msg'];


date_default_timezone_set('Asia/Baghdad');

$nw_date=date("F j, Y");

$body="name: ".$name."\n msg: ".$msg."\n date: ".$nw_date;

$email="$email";

mail("rezult.smtp@gmail.com", "new msg",$body, "from:  <".$email.">");


echo "<span class='eco'>thanks for contact<br></span>";

echo "<span dir='rtl'>- </span>"."<a href='contact.php'>back..</a>";

?>

so i expect the problem of empty message is because

else {

                    echo'<script>window.location = "process.php";</script>';

so i removed it and put process.php in the action

<form dir="rtl" action="" method="post">

it's worked and the message arrived fine

but the validation didn't work

so how i can make the code and the validation executed before go to next page .. ?

i know how to do this by javascript

but can i do it in this code ? what i missed ?

thanks a lot .. and sorry for long question

Amit Joki
  • 53,955
  • 7
  • 67
  • 89
belal879
  • 11
  • 3
  • 2
    `window.location` causes the specified page to fetched as a GET request, which means $_POST will be empty. – Marc B Mar 23 '14 at 03:37
  • You might take a look at http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit to get around the problem that @MarcB identified – Floris Mar 23 '14 at 03:41

2 Answers2

0

Actually. You don't need that process.php so copy the code in process.php and paste it in the else statement. Use this code use $submitted variable. i have made some changes in the code and i have marked it with comment // new code

<?


    $submitted = "";   // new code     

  $name=$_POST['name'];
                        $email=$_POST['email'];
                        $msg=$_POST['msg'];

                    if($_POST['submit']){

                        if(empty($name))

                            $errorname = ("<span id='error'>no name</span>");



                        if(empty($email))

                            $erroremail = ("<span id='error'>no email</span>");



                        if (empty($msg))

                            $errormsg = ("<span id='error'>no msg</span>");




                            else {


$name=$_POST['name'];

$email=$_POST['email'];

$msg=$_POST['msg'];


date_default_timezone_set('Asia/Baghdad');

$nw_date=date("F j, Y");

$body="name: ".$name."\n msg: ".$msg."\n date: ".$nw_date;

$email="$email";

mail("rezult.smtp@gmail.com", "new msg",$body, "from:  <".$email.">");


echo "<span class='eco'>thanks for contact<br></span>";

echo "<span dir='rtl'>- </span>"."<a href='contact.php'>back..</a>";

$submitted = "yes";   // new code
                                }
                        }




if($submitted == ""){   // new code  ?>


<form dir="rtl" action="" method="post">
                            <div>name<span class="nothing">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><input type="text" name="name" id="name" dir="rtl"> 


            <span id="errororg">&nbsp;<?= $errorname; ?></span></div>
                            <div >email<span class="nothing">&nbsp;&nbsp;</span><input  type="email" color="#fff" name="email" id="email"><span id="errororg">&nbsp;&nbsp;<?= $erroremail; ?></span></div>
                            <span>msg</span>
                            <div><textarea rows="10" name="msg" id="comment" dir="rtl"></textarea><span id="errororg"><?= $errormsg; ?></span></div>
                            <div><input type="submit" name="submit" value="send" dir="rtl"></div><br>
                        </form>
<?php }  // new code ?>
Ananth
  • 1,438
  • 3
  • 14
  • 27
  • exactly i made the code in same page and worked fine but the reason why i made the code in new page that i want when this message showed echo "thanks for contact
    "; echo "- "."back.."; i want to hide the form u understand me ? can i use exit() to do this ?
    – belal879 Mar 23 '14 at 04:00
  • if you want to hide the form after submission, then use exit; next to "thanks for contact message" . check the above code. – Ananth Mar 23 '14 at 04:06
  • yes it's worked but the footer and every thing down "exit()" has hide so can i do anything about it ... and thanks a lot for the help – belal879 Mar 23 '14 at 04:11
  • so that why i do 2 page because i made the 2 page like 1 but removed the form .. you underestand me .. – belal879 Mar 23 '14 at 04:13
  • i have made some changes in the code and i have marked it with comment // new code – Ananth Mar 23 '14 at 04:15
  • thanks thanks a lot much lot ... it's worked and everything i want it it work ... thaanks – belal879 Mar 23 '14 at 04:36
0

If you want to redirect the page after the form is submitted successfully without any message set <form action="" method="post"> and in your code without deleting that javascript call change it to this:

$name=$_POST['name'];
$email=$_POST['email'];
$msg=$_POST['msg'];
$error = array();

if($_POST['submit']){
   if(empty($name)) {
      $error['name'] = "<span id='error'>no name</span>";
   }
   if(empty($email)) {
      $error['email'] = "<span id='error'>no email</span>";
   }
   if (empty($msg)) {
      $error['msg'] = "<span id='error'>no msg</span>";
   }

   if (empty($error)) {
      echo'<script>window.location = "process.php";</script>';
   }
}

And this will be your form

<form dir="rtl" action="" method="post">
   <div>name<span class="nothing">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><input type="text" name="name" id="name" dir="rtl"> 
   <span id="errororg">&nbsp;<?= !empty($error['name']) ? $error['name'] : ''; ?></span></div>
   <div >email<span class="nothing">&nbsp;&nbsp;</span>
       <input  type="email" color="#fff" name="email" id="email">
       <span id="errororg">&nbsp;&nbsp;<?= !empty($error['email']) ? $error['email'] : ''; ?></span></div>
   <span>msg</span>
   <div><textarea rows="10" name="msg" id="comment" dir="rtl"></textarea>
        <span id="errororg"><?= !empty($error['msg']) ? $error['msg'] : ''; ?></span></div>
   <div><input type="submit" name="submit" value="send" dir="rtl"></div><br>
</form>
Javad
  • 4,153
  • 2
  • 19
  • 35