too long

I'm trying to change my form action in my HTML then submit using javascript.

The conditions are in PHP .

I need help if anyone can assist me.

This is my PHP function :-

<?php
error_reporting(0);

if(isset($_POST['email'])){
    $email=$_POST['email'];
   if(!empty($email)) {
       $chrono = 0;
    } else {
       $chrono = 1;
    }
}
?>

The motive of the PHP is to check null email entry.

Here's the javascript function :-

<script type="text/javascript"> 
    function fireform(val){ 
    // missing codes
      document.forms["demoform"].action=val; 
      document.forms["demoform"].submit(); 
    } 
    // missing codes    
</script>

HTML :-

<form name="demoform" action="">        
<input type ="text" name="name" id="name">             
<input type="hidden" name="buttonpressed" id="buttonpressed"> 
<input type="button" value="submit B" onclick="fireform('b')">

I want to do it in a way , when the user entered an empty email , the PHP will read it as chrono = 0. Then goes to javascript , if the chrono equal to 0 , the action will remain empty.

If the chrono = 1 , the javascript will change the action of the form and submit.

I need help thanks.

I would recommend checking the email field (for being empty) in javascript, and when you have set the proper action submit the form in javascript.

Check the field:

$('#<enter id of field>').val();

Update the action:

$('form').attr('action', 'Enter your updatet action here');

Submit the form:

http://api.jquery.com/submit/

Your flow is unclear: it seems that you want to change the form action from PHP, but PHP is triggered after the form submission. So there's something weird in your flow. You also don't seem to have a field called email in your markup. Add it (or rename the field name):

<input type="text" name="email" id="email">

Nonetheless, having an empty action means the form will be submitted to the page itself.

Probably what you need is a client side validation of the email field. In the fireform() JavaScript function, just add a check for email field:

function fireform(val){
    if (document.forms["demoform"].email.value.length > 0){
        document.forms["demoform"].action = val; 
        document.forms["demoform"].submit();
    }
}

This should be enough to get what you need.