简体   繁体   中英

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.

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>

如果要执行PHP函数,则必须将数据发送到服务器。PHP函数将不会在用户浏览器上执行,因此<button class="btn btn-default" id="change_formulaire" onclick="switch_tab1();<?php Verif_date_admin(); ?>" data-toggle="tab">Valider</button>将不起作用,而是在switch_tab1()函数中通过ajax请求执行PHP函数,并Verif_date_admin()删除PHP函数Verif_date_admin()按钮点击事件

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM