-1

I want to send an email and save the data into the data base as well. But the saved data is as name = <script>document.write(name)</script> and so on. Please help me solve this problem. Thanks

I have tried a number of things to solve this problem. In fact I tried to echo first of all the js variables than save them in php variables but failed to do so.

function submit_form(){
    name = document.getElementsByName("name")[0].value;
    email = document.getElementsByName("email")[0].value;
    f_email = document.getElementsByName("f_email")[0].value;
    filter = document.getElementsByName("filter")[0].value;
    if(name == ''){
        alert('Pleas Enter Your Name');
    }else if(email == ''){
        alert('Pleas Enter Your Email');
    }else if(!(validateEmail(email))){
        alert('You Must Enter a Valid Email Address');
    }else if(f_email == ''){
        alert("Pleas Enter the Reciever's Email");
    }else if(!(validateEmail(f_email))){
        alert('You Must Enter a Valid Email Address');
    }else if((filter != "islamabad") && (filter != "Islamabad")){
        alert("The Answer of the Question is Wrong.");
    }else{
        //query_submit();
        <?
            if(($e_id > 0)){//ef_id
             $name = "<script>document.write(name)</script>";

             $email = "<script>document.write(email)</script>";

             $f_email = "<script>document.write(f_email)</script>";

            $query ="INSERT INTO email_sent (name, email, f_email, unsubscribed,datetime)
                        VALUES 
                        (\"".$name."\", \"".$email."\", \"".$f_email."\", 0,NOW()) "; ?>
                <? if($result = $DB->DB_Query($query)){
                    //echo $name." ==ppp";
                    $query_select_m ="SELECT * FROM post_m where pj_id =".$pj_id; ?>
                    <? $result_select_m = $DB->DB_Query($query_select_m); 
                    if(($result_select_m)){
                        $m_title = $result_select_m[0]['m_title'];
                        $m_desc = $result_select_m[0]['m_description'];
                        $m_skills = $result_select_m[0]['m_skills'];
                        sending_email($fromname, $fromaddress, $toname, $toaddress, $subject, $message); 
                        //want to send email
                        ?>
                            alert("selection Query");
                        <?
                    }
                ?>
                         alert("Query Executed Properly!eeee");
                         //alert("<?echo json_encode($name); ?>");

                    <? }else{ ?>
                         alert("Unable to execute");
                    <? }
            }else{ ?>
                alert("Here we are");
            <? }
        ?>


        alert("Done");

        document.getElementsByName("name")[0].value = '';
        document.getElementsByName("email")[0].value = '';
        document.getElementsByName("f_email")[0].value = '';
        document.getElementsByName("filter")[0].value = '';
        //alert(name_for_e);
        //set_values(name_for_e, email_for_e, f_email_for_e);
    }
}
safeer008
  • 323
  • 4
  • 8

2 Answers2

4

You can't do that, php is a Server language and Javascript is a Cient Language. So the PHP will executed before it is sent to the client and javascript will be executed by the client.

You have to sent the data by post or get HTTP Command, validate the data and then put it in de database.

Here are more Information: http://www.php.net/manual/en/tutorial.forms.php

schnawel007
  • 3,982
  • 3
  • 19
  • 27
0

You can't mix JavaScript and PHP that way.

On your HTML Form put a "onclick="javascript:submit_form()"; on your submit button and when all checks are passed "return true;" (instead of //query_submit(); and the rest)

Also in your HTML Form set a Form target where you catch all the Form Data to process them with PHP. Here is a complete example: http://www.php.net/manual/de/tutorial.forms.php

And here one with an additional validation: http://hibbard.eu/simple-form-validation-in-php/

qualle
  • 1,347
  • 3
  • 14
  • 29