Tripesdeporc
Tripesdeporc

Reputation: 94

Call two functions from javascript and php by clicking button


I have a little problem : I have two functions, one that verify a date in PHP and another that act on div properties in javascript. I would like to call both of them by clicking the button but I can't...
I am surely needing some help.
This is my code :

js

function switch_tab1()
{
    if(document.getElementById("radio_mater_oui").checked==true && (document.getElementById("debut_mater").value == "" || document.getElementById("fin_mater").value == ""))
    {
        document.getElementById("erreur_champs_admin").style.display="";
    }
    else
    {
        tab1_tab2();
    }
}

html button

<button class="btn btn-default" id="change_formulaire" onclick="switch_tab1();<?php Verif_date_admin(); ?>" data-toggle="tab">Valider</button>

php

<?php
function Verif_date_admin()
{
    if(strlen($_SESSION["debut_mater"]) == 10 && strlen($_SESSION["fin_mater"]) == 10)
    {
        // Division des chaînes dans 3 variables chacune afin de reconstituer une chaîne
        // valide pour l'insertion dans la base de données au format anglais
        list($debut_jour,$debut_mois,$debut_annee) = explode('-',$_POST["debut_mater"]);
        list($fin_jour,$fin_mois,$fin_annee) = explode('-',$_POST["fin_mater"]);
        $debut_mater_valide = checkdate($debut_mois,$debut_jour,$debut_annee);
        $fin_mater_valide = checkdate($fin_mois,$fin_jour,$fin_annee);
        // Test de validité des booléens assignés
        if($debut_mater_valide != 1 || $fin_mater_valide != 1)
        {
            echo '<script>document.getElementById("date_format_admin").style.display="";</script>';
        }
        else
        {
            $debut_mater = $debut_annee.'-'.$debut_mois.'-'.$debut_jour;
            $fin_mater = $fin_annee.'-'.$fin_mois.'-'.$fin_jour;
        }
    }
    else
    {
        echo '<script>document.getElementById("date_format_admin").style.display="";</script>';
    }
}
?>

Don't take care of french comments, it is for a rectorship.

Upvotes: 1

Views: 100

Answers (2)

Skalbhile
Skalbhile

Reputation: 156

Can you please remove <script> tag from you echo statement and try it again.. also you need to use single quotes in next statement

<script>document.getElementById("date_format_admin").style.display="";</script>

when it renders in html it should be like

<button class="btn btn-default" id="change_formulaire" onclick="switch_tab1();

document.getElementById('date_format_admin').style.display=''" data-toggle="tab">Valider</button>

Upvotes: 1

Gaurav
Gaurav

Reputation: 56

if you want to execute the PHP function you have to send the data to the server .. PHP function will not execute on user browser so <button class="btn btn-default" id="change_formulaire" onclick="switch_tab1();<?php Verif_date_admin(); ?>" data-toggle="tab">Valider</button> will not work instead of this you execute the PHP function by ajax request in switch_tab1() function and remove PHP function Verif_date_admin() from button click event

Upvotes: 0

Related Questions