AJAX DISPLAY PHP

I come to you, I have a car, I cannot show page.php dynamically without reloading the page.

I will explain the concept to you, I have a menu, and from that menu, I want that as soon as I click on the topic, load my page.php and I will dynamically render it without reloading it. I tried ajax but i cant

page.php:

    <?php
if($_POST['action'] == 'call_this'){
@session_start();
include ('db_connect.php');
 include('function.php');
$chainefinal = explode ('?',$_SERVER['REQUEST_URI']);
/*-------------------------------------------------Initialisation des variables---------------------------------------------------------------------------------------------*/
$t1=$t2=$t3=$t4=$top=$output=$ligne1=$ligne2=$final='';
$total_sheduled_fr=$total_sheduled_de=$total_sheduled_fr_FMS=$total_sheduled_fr_CPT=$total_sheduled_de_FMS=$total_sheduled_de_CPT= 0;
$current_h = date("H");

/************************************************SELECTION JOURS, MOIS, ANNEE *******************************************************************/

 //$annee_selectionne = isset($_GET['year']) ? $_GET['year'] : date('Y');
 //var_dump($annee_selectionne);
 //$mois_selectionne = isset($_GET['month']) ? $_GET['month'] : date('m');
 // $jour_selectionne = isset($_GET['day']) ? $_GET['day'] : date('d');
 // var_dump($jour_selectionne);

    $date = date("Y/m/d", strtotime("this thursday UTC"));
    $date1 = split('/', $date);

    $annee_selectionne = $date1[0];
    // var_dump($annee_selectionne);
    $mois_selectionne = $date1[1];
    // var_dump($mois_selectionne);
    $jour_selectionne = $date1[2];
    // var_dump($jour_selectionne);

    $today = date("jS F, Y", strtotime("this monday UTC")); 
    $today1 = date("jS F, Y", strtotime("this tuesday UTC"));
    $today2 = date("jS F, Y", strtotime("this wednesday UTC"));
    $today3 = date("jS F, Y", strtotime("this thursday UTC"));
    $today4 = date("jS F, Y", strtotime("this friday UTC"));

  ################################################ Initialisation de nouvelle variable ###############################

    $tday = mktime(0, 0, 0, $mois_selectionne, $jour_selectionne , $annee_selectionne);
    $change_limit = mktime(date("H"), 0, 0, date("m"), (date("d") + 6), date("y"));
    $search_date = $annee_selectionne.'-'.$mois_selectionne.'-'.$jour_selectionne;

    // a garder -->  $search_date_plus_5_jours = strtotime("today + 5 days ");
    // echo strtotime("today + 5 days "), "\n";
    // a garder --> echo $search_date_plus_5_jours;  


/*--------------------------------------------FORMAT HEURE CALENDRIER EN HAUT TABLEAU -------------------------------------------------------------------*/
    $requette = $bdd->query('SELECT * FROM trainning_format WHERE trainning_date = \''.$search_date.'\'');
    $debut =8;
    $fin=18;

/***************************  Affichage heure haut du tableau *****************************/

 if (!$data = $requette->fetch())
      {
         $requette = $bdd->query('SELECT * FROM trainning_format WHERE trainning_date < \''.$search_date.'\'');
         $data = $requette->fetch();
         $debut = 8;
         $fin = 18;

     }
 else
    {
        if ( $data['trainning_format'] == 1)
            {
                $debut = 8;
                $fin = 18;
            }
        else
            {
                $debut = 6;
                $fin = 22;
            }
    }
  $requette->closecursor();

/*--------------------------------------------------------------------------------------------------------------*/
/*------------------------------------------- en tete du planning ----------------------------------------------*/
/*--------------------------------------------------------------------------------------------------------------*/
/****************************** Commencement et fin des lignes du tableu(heure) **************************/
for ($h = ($debut - 1);$h <= ($fin + 1);$h++)
    {
        $ligne1 .= '<td colspan="4" class="emptyhead">
                    '.$h.'<br />|
                    </td>
                    ';  
    }

    /****************************** Commencement et fin des lignes en blanc **************************/
for ($h = ($debut - 1);$h <= ($fin+2);$h=$h+0.25)
    {
        if ($h == ($current_h + 0.75) AND $jour_selectionne == date("d"))
            {
                $ligne2 .= '<td class="smallplanningnow">....</td>
                    ';
            }
        else
            {
                $ligne2 .= '<td class="smallplanning">....</td>
                ';
            }
    }
/*--------------------------------------------------------------------------------*/
/*---------------------------- colone de STH -------------------------------------*/
/*--------------------------------------------------------------------------------*/
if (isset($_SESSION['droit']) and $_SESSION['droit'] == 5 or $_SESSION['droit'] == 2)
    {
        $ligne1 .= '<td>GE</td>
                    <td>FR</td>';
        $ligne2 .= '<td class="smallplanning">....</td>
                    <td class="smallplanning">....</td>';
    }
/*--------------------------------------------------------------------------------*/
/*-------------------------- Fin colone de STH -----------------------------------*/
/*--------------------------------------------------------------------------------*/
$ligne_H =$ligne1.'</tr><tr>'.$ligne2.'</tr>
            </thead>';

/*--------------------------------------------------------------------------------*/
/*---------------- Affichage SIMULATEUR T1 ; T2 ; T3 ; T4 ------------------------*/
/*--------------------------------------------------------------------------------*/

for ($b = 1;$b <= 4;$b++)
    {
        $requette   =   $bdd->query('   SELECT simulator.building_id, level_1.name
                                    FROM simulator, level_1
                                    WHERE level_1.id = simulator.building_id
                                    AND simulator.building_id = '.$b);
        $building = $requette->fetch();
        $tranche = $building['name'];
        $requette->closecursor();

/*--------------------------------------------------------------------------------------------------------------*/
/*----------------------------------------- Affichage du planning ----------------------------------------------*/
/*--------------------------------------------------------------------------------------------------------------*/
        $output .='<table>
                    <thead>
                        <tr >
                            <td class="build2">
                                '.$tranche.'
                            </td>'.$ligne_H.'<tbody>';

        $requette   =   $bdd->query('   SELECT simulator.*, kit.kit_name
                                        FROM simulator, kit
                                        WHERE kit.id = simulator.kit
                                        AND simulator.building_id = '.$b.'
                                        ORDER BY efa_name DESC');

/*--------------------------------------------------------------------------------------------------------------*/
/*----------------------------------------- Affichage des noms de simulateurs ----------------------------------*/
/*--------------------------------------------------------------------------------------------------------------*/

        while ($sim =   $requette->fetch())
            {
                $cabtype = substr($sim['kit_name'],0, 1);  /* --> Affichage lettre P & G */
                $req = $bdd->query('SELECT nation
                                    FROM four_week_planning
                                    WHERE simulator_id = '.$sim['id'].'
                                    AND planning_date = \''.$search_date.'\'');

                if($don = $req->fetch())
                    {
                        $nation = $don['nation'];
                        $req->closecursor();
                    }
                /*------------------- si rien dans la bdd à cette date -------------*/
                else
                    {
                        $reqs = $bdd->query('   SELECT nation FROM four_week_planning
                                                WHERE simulator_id = '.$sim['id'].'
                                                AND planning_date < \''.$search_date.'\'
                                                ORDER BY planning_date DESC LIMIT 0,1');
                        $dons = $reqs->fetch();
                        $nation = $dons['nation'];
                        $reqs->closecursor();
                    }
                $req->closecursor();

/*--------------------------------------------------------------------------------------------------------------*/
/*----------------------------------------- Affichage des DRAPEAU DE ET FR -------------------------------------*/
/*--------------------------------------------------------------------------------------------------------------*/

                switch($nation)
                    {
                        case 1:$class = 'class="de"';$nat = 0;break;
                        case 2:$class = 'class="fr"';$nat = 2;break;
                        case 3:$class = 'class="de_disable"';$nat = 3;break;
                        case 4:$class = 'class="fr_disable"';$nat = 3;break;
                        default:$class = '';$nat = 0;break;
                    }


/*--------------------------------------------------------------------------------------------------------------*/
/*---------------- Affichage Icone tableau ; Class = drapeau ; sim = nom du simu ; cab = P ou G ----------------*/
/*--------------------------------------------------------------------------------------------------------------*/

                $output .= '    <tr class="">
                                <td '.$class.'  >
                                    '.$sim['stpname'].'<br />'.$cabtype.'
                                </td>
                            ';
                $h = $debut - 1.25;
                $debut_sec = ($debut - 1.5) * 3600;
                $f = $fin + 1.5;
                $first = 'first';
                while ($h <= $f)
                    {

/*--------------------------------------------------------------------------------------------------------------*/
/*---------------- Récupération de la valeur du champ heure (7 ; 7.15 ; 7.30 ; 7.45 ; 8 ect ... ----------------*/
/*--------------------------------------------------------------------------------------------------------------*/
                        $c = $h - 0.25 ;
                        $minutage = $h - intval($h);
                        if($minutage != 0)
                            {
                                switch($minutage)
                                    {
                                        case 0.25:$minute = 15;break;
                                        case 0.50:$minute = 30;break;
                                        case 0.75:$minute = 45;break;

                                    }
                            }
                        else
                            {
                                $minute = 0;
                            }
                        $current_time = mktime(intval($h), $minute, 0, $mois_selectionne, $jour_selectionne, $annee_selectionne);
                        if(isset($_SESSION['droit']) AND (($_SESSION['droit'] >= 2 AND $current_time >= $change_limit AND $_SESSION['nation'] == $nat) OR $_SESSION['droit'] >= 2))
                            {
                                $edit_add ='<a href="javascript:openPlanningPopUp(\'simplaninng/index.php\',\''.$sim['id'].'\',\''.$c.'\',\''.$jour_selectionne.'\',\''.$mois_selectionne.'\',\''.$annee_selectionne.'\')">&otimes;</a>';
                            }
                        else
                            {
                                $edit_add ='';
                            }
                        if ( is_int($h) AND $jour_selectionne == date("d"))
                            {
                                $cur_class = 'emptybodyT';
                            }
                        else
                            {
                                $cur_class = 'emptybody';
                            }
                        $heure = $tday + $debut_sec ;
                        $req    =   $bdd->query('   SELECT simplanning.*,tigerUsers.trigramme
                                                    FROM simplanning,tigerUsers
                                                    WHERE simplanning.sim_id = '.$sim['id'].'
                                                    AND simplanning.date_start ='.$heure.'
                                                    AND tigerUsers.id=simplanning.user_id');

                        if($simuse =$req->fetch())

                            {

                                $fi = $simuse['date_end'];
                                $deb = $simuse['date_start']; 
                                $delta = ($fi - $deb) / 900;

                                /*----------------------------------------------------------------------*/
                                /*-------------------------- calcul sth day-----------------------------*/
                                /*----------------------------------------------------------------------*/

                                if ($simuse['trainning_type'] != 7 AND $simuse['trainning_type'] != 10 AND $simuse['trainning_type'] != 11 AND $simuse['trainning_type'] != 17 AND $simuse['trainning_type'] != 12 AND $simuse['trainning_type'] != 13 AND $simuse['use_mode'] == 1)
                                    {
                                        $deta = ($fi - $deb);
                                        if ($nat == 2)
                                            {
                                                $total_sheduled_fr = $total_sheduled_fr + $deta;
                                                $total_sheduled_de = 0;
                                            }
                                        else
                                            {
                                                $total_sheduled_de = $total_sheduled_de + $deta;
                                                $total_sheduled_fr = 0;
                                            }
                                    }
/*--------------------------------------------------------------------------------------------------------------*/
/*---------------- Affichage de la fédération qui utilise le simulateur  ---------------------------------------*/
/*--------------------------------------------------------------------------------------------------------------*/

                                $vdb = display_vdb($simuse['request_vdb']);     /* vdb = visuel data base = le luc --> fonction.php */
                                $type = trainning_type($simuse['trainning_type'],'type',$simuse['use_mode']);
                                $classmode = trainning_type($simuse['trainning_type'],'class',$simuse['use_mode']);
                                if ($type == 'Del.')
                                    {
                                        $type .=' the '.date_format(date_create($simuse['last_update']), 'd-m H:i');    
                                    }
                                else
                                    {
                                        $type .= '<br /><font size="-5">'.$vdb.'</font>';
                                    }
                                if ($simuse['request_mode'] == 2)
                                    {
                                         $classmode = $classmode.'coupled';
                                    }
                                 switch($simuse['fede_wan_amtlt'])
                                     {
                                         case 0 : $type .= '';break;
                                         case 1 : $type .= '<br /><a class="opt">Fede</a>';break;
                                         case 2 : $type .= '<br /><a class="opt">Wan</a>';break;
                                         case 3 : $type .= '<br /><a class="opt">AMTLT</a>';break;
                                         case 4 : $type .= '<br /><a class="opt">R.IOS</a>';break;
                                     }
/*--------------------------------------------------------------------------------------------------------------*/
/*---------------- Si on a les droits alors on peut modifié ou supprimer un training  --------------------------*/
/*--------------------------------------------------------------------------------------------------------------*/

                                if(isset($_SESSION['droit']) AND (($_SESSION['droit'] >= 2 AND $_SESSION['nation'] == $nat AND $current_time >= $change_limit) OR $_SESSION['droit'] >= 2))
                                    {
                                        $edit_add ='<div class="plan_edit"></div>';
                                        $dele = '<div class="plan_delete"><a href="javascript:PlanningDeletPopUp(\''.$simuse['id'].'\')" >&otimes;</a></div>';

                                    }
                                else
                                    {
                                        $edit_add ='';
                                        $dele = '';
                                    }
/*--------------------------------------------------------------------------------------------------------------*/
/*----------------------------------Bouton Modifier et Delete  -------------------------------------------------*/
/*--------------------------------------------------------------------------------------------------------------*/

                                $output .= '    <td colspan="'.$delta.'" class="'.$classmode.'" title="Add by: '.$simuse['trigramme'].' '.(($simuse['last_update']!=$simuse['input_date'])?'updated on '.$simuse['last_update']:'Add on '.$simuse['input_date']).'">
                                                <div>'.$edit_add.''.$dele.'</div>
                                                '.$type.'
                                                <br />'.$simuse['commentaire'].'
                                            </td>'; 
                                $debut_sec = $debut_sec + (900 * ($delta + 1));
                                $h = $h + (0.25 * ($delta + 1) ) ;

/*--------------------------------------------------------------------------------------------------------------*/
/*------------ Reservation 15 min apres le training pour la maintenance ; MENU a gauche  -----------------------*/
/*--------------------------------------------------------------------------------------------------------------*/

                                if ($h == $current_h+0.5 AND $jour_selectionne == date("d"))
                                    {
                                        $output .= '<td class="'.$cur_class.'now"></td>
                                        ';
                                    }
                                else
                                    {
                                        $output .= '<td class="'.$cur_class.'"></td>
                                        ';
                                    }
/********************************************************************************************************************/                                      
                            }
                        else
                            {
                                if ($h == $current_h+0.25 AND $jour_selectionne == date("d"))
                                    {
                                        $output .= '<td class="'.$cur_class.'now">'.$edit_add.'</td>
                                        ';
                                    }
                                else
                                    {
                                        $output .= '<td class="'.$cur_class.'">'.$edit_add.'</td>
                                        ';
                                    }
                                $debut_sec = $debut_sec + 900;
                                $h = $h + 0.25;
                            }
                        $req->closecursor();
                    }

                /*--------------------------------------------------------------------------------*/
                /*---------------------------- colone de STH -------------------------------------*/
                /*--------------------------------------------------------------------------------*/

                if (isset($_SESSION['droit']) and $_SESSION['droit'] == 5 or $_SESSION['droit'] == 2)
                    {
                        $output .= '<td class="emptyhead">'.affich_heure(($total_sheduled_de)/ 3600 ).'</td>';
                        $output .= '<td class="emptyhead">'.affich_heure(($total_sheduled_fr)/ 3600 ).'</td>';
                    }
                $total_sheduled_fr = 0;
                $total_sheduled_de = 0;

                /*--------------------------------------------------------------------------------*/
                /*-------------------------- fin colone de STH -----------------------------------*/
                /*--------------------------------------------------------------------------------*/

                 $output .= '</tr>
                 ';
            }
        $requette->closecursor();
        $output .='</table>';
        if ($b != 5)
            {
                $output .= '<br />';
            }
        $final .= $output;
        $output ='';
    }
?>
<style type="text/css">   

ul#menu_horizontal li { 
display : inline;
padding : 0 0.5em; /* Pour espacer les boutons entre eux */

}
ul#menu_horizontal {
list-style-type : none; /* Car sinon les puces se placent n'importe où */
} 
</style>
<h1 id="D4"> <?php echo $today3; ?></h1>

<ul id="menu_horizontal">

<li><a href="#D1"><?php echo $today ?></a></li>
<li><a href="#D2"><?php echo $today1 ?></a></li>
<li><a href="#D3"><?php echo $today2 ?></a></li>
<li><a href="#D4"><?php echo $today3 ?></a></li>
<li><a href="#D5"><?php echo $today4 ?></a></li>

</ul>
 <div class="plansimcalendar" id="test2">
    <?/*php/* include ('C_User_file/calendar.php');*/?>
</div> 
<br /><br />
<!--<div class="simplanning_left">
    <a href="index.php?action=simplan&day=<?php echo $jour_link_moins;?>&month=<?php echo $mois_link_moins;?>&year=<?php echo $annee_link_moins;?>">-</a>
</div>-->
<div class="simplanning_center">
<!-- <a href="simplaninng/filesimplanning.php">&otimes;</a> -->
<?php echo $final; ?>
</div>
<!--<div class="simplanning_right">
    <a href="index.php?action=simplan&day=<?php echo $jour_link_plus;?>&month=<?php echo $mois_link_plus;?>&year=<?php echo $annee_link_plus;?>">+</a>
</div>-->

}

      

Ajax:

    <script>
  function myAjax(){
   $.ajax({
       type= "POST",
  url: 'index.php?action=simplan3",
  datatype: 'json',
  data: {action:'call_this'},
  success: function(data) {
    $('#result').php(data);
    alert('Load was performed.');
  }
});
  }


  </script>
  <div id="result">

  </div>
<li><a href="#D2" id="merde" onclick="myAjax()"><?php echo $today1 ?></a></li>

      

thanks for the help

+3


source to share


2 answers


Some error in the ajax call:

    <script>
      function myAjax(){
       $.ajax({
           type: "POST", //not type= "POST"
      url: 'index.php?action=simplan3',
      datatype: 'json',
      data: {action:'call_this'},
      success: function(data) {
        $('#result').html(data);//or $('#result').text(data);
        alert('Load was performed.');
      }
    });
return false; // just outside the ajax call would help you i guess
      }    
      </script>

      

I found the problem: double quote is used in ajax which was causing the error. check below:



  url: 'index.php?action=simplan3",

      

changed to

  url: 'index.php?action=simplan3',

      

+1


source


You have to add return false;

to your ajax function, this will prevent the page from reloading.

Here's an example implementation:



<script>
$("form").submit(function(){
        //alert("Submitted");
        //prevent from redirect
        return false;
    });



  </script>

      

+2


source







All Articles