-1

Hi I am trying to execute a php function inside a javascript function. This javascript function is a form validation method which is called on through onclick method. This function validates the form fields and inserts the data into the mysql database.

I am calling this php function only when the form values are validated. The problem is that even when the form entries are not validated, that is, even when i enter wrong data the function is being called.

However, if dont call the function here inside the script and i enter the php inside the body the validation works.

jobs.php:

<script type="text/javascript">
        function validate(){
            var name= document.forms["Jform"]["name"];
            var email= document.forms["Jform"]["email"];
            var Experience= document.forms["Jform"]["experience"];
            var letters=/^[A-Za-z]+$/;
            var boolean=true;
            var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
            if(name.value == ""){
                alert("Please enter your name");
                name.focus();
                boolean=false;
                return false;
            }
            if((!email.value.match(mailformat)) || email.value==""){
                window.alert("Please enter proper email");
                email.focus();
                boolean=false;
                return false;
            }
            if(Experience.value==""){
                window.alert("Please enter valid characters");
                email.focus();
                boolean=false;
                return false;
            }

            if(boolean){
                <?php 
                    insertfunc();
                ?>
            }
        }
    </script>

This is my code.

To rephrase, i want the php function to execute only when the form values are validated.

Could someone please help

Dhirish
  • 321
  • 5
  • 14
  • 3
    Doesn't work that way. You need to call your php function via an AJAX call. –  Apr 08 '18 at 07:25
  • could please tell me how i am supposed to do that – Dhirish Apr 08 '18 at 07:26
  • 1
    nosense as written here. please read first about server-side and client-side language. Both can't be mixed... There are plenty of books about that, and of course online resource. ; voting for closing post as the missing knowledge is too broad for a post answer – Pierre Apr 08 '18 at 07:26
  • @Dhirish What does insertfunc() do? – Jakub Judas Apr 08 '18 at 07:28
  • Possible duplicate of [How can I call PHP functions by JavaScript?](https://stackoverflow.com/questions/15757750/how-can-i-call-php-functions-by-javascript) – user202729 Apr 08 '18 at 07:30
  • @JakubJudas it inserts data into the db – Dhirish Apr 08 '18 at 07:31
  • is there a possible way that i could send the boolean value again back to the same page? – Dhirish Apr 08 '18 at 07:34
  • 1
    @Dhirish In that case, the easiest way would be to create another php script (let's call it insert.php) and then send the data to that script using ajax - if you have jQuery include, you can use https://api.jquery.com/jquery.post/ (example: $.post( "insert.php", { name: name, email:email } ); – Jakub Judas Apr 08 '18 at 07:35
  • @Dhirish The practical problem with your original code is that PHP is eecuted on the server and then the response is sent to the user's browser, where javascript is executed. These two cannot cooperate any more - in fact, at the time of javascript execution, the PHP script has already finished and been terminated so it is no longer even running. – Jakub Judas Apr 08 '18 at 07:38
  • OK @JakubJudas thank you! – Dhirish Apr 08 '18 at 07:42
  • Hi @JakubJudas i tried using window.location.replace("jobs.php?boolean=true"); return false; – Dhirish Apr 08 '18 at 09:01
  • but this time only empty values are being stored inside the db – Dhirish Apr 08 '18 at 09:02
  • @Dhirish If that is the only thing you changed, then it cannot work. There is no check in PHP for the value boolean. I think you still do not undertand the concept properly. You absolutely need to separate PHP and javascript completely. PHP will never see javascript variables. It might actually help you if you force yourself not to write javascript code and php code in one file. – Jakub Judas Apr 08 '18 at 09:25

1 Answers1

0

Hi Try this but don't assume the exact code will work, you have to debug it as per your need -

Assuming your php response will be JSON format with 200 status code.
You jobs.php file's JS code -

<script type="text/javascript">
    $('document').ready(function(){
        // Your JS Ajax function to call insertfunc.php where your php code resides
        function insertData() {
            var nameValue = document.forms["Jform"]["name"];
            var emailValue = document.forms["Jform"]["email"];
            var experienceValue = document.forms["Jform"]["experience"];

            $.ajax({
                url: "<your-path-to>/insertfunc.php",
                dataType: "json",
                data: {
                    name: nameValue, // Get name value as $_REQUEST["name"] in PHP
                    email: emailValue,
                    experience: experienceValue,
                },
                success: function(result) {

                    if(result.code == 200) {
                        alert("Success");
                    }
                }
            });
        }

        function validate(){
            var name= document.forms["Jform"]["name"];
            var email= document.forms["Jform"]["email"];
            var Experience= document.forms["Jform"]["experience"];
            var letters=/^[A-Za-z]+$/;
            var boolean=true;
            var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
            if(name.value == ""){
                alert("Please enter your name");
                name.focus();
                boolean=false;
                return false;
            }
            if((!email.value.match(mailformat)) || email.value==""){
                window.alert("Please enter proper email");
                email.focus();
                boolean=false;
                return false;
            }
            if(Experience.value==""){
                window.alert("Please enter valid characters");
                email.focus();
                boolean=false;
                return false;
            }

            if(boolean){
                // Call your Ajax function here
                insertData();
            }
        }
    });
</script>

Keep your insertfunc.php where your php insert code resides.
Eg -

<?php 
    $name = $_REQUEST["name"];
    //...
    // Your php insert functionality
?>
Varad Mondkar
  • 1,441
  • 1
  • 18
  • 29