aleixrav
aleixrav

Reputation: 214

Using only one js file for multiple php ajax calls

First of all, my intention is to have 1 js file where there are multiple ajax calls. I want these ajax calls, which are php files, to have the same js file inside of it but not doing another request, which makes it to run slow after any click.

Some of my main php file:

<head>
    <script src="<?php echo $_SESSION['url'] ?>js/direcinfo.js"></script>
</head>
<body>

    <div class="direcciones">
        <a href="javascript:void(0)" id="c_menu_direcciones" class="c-menu c_menu_direcciones">
            <p>
              Direcciones
            </p>
        </a>
    </div>
    <div id="cuenta_menu_direcciones" class="c-menu-content">
        <h1>Direcciones de correo</h1>
        <div id="expand_wrapper_dir">
        </div>
    </div>
</body>

Js file (direcinfo.js):

$('#c_menu_direcciones').click(function(){
   $.ajax({
      dataType: "html",
      type: "POST",
      url: "direcalias.php",
      success: function(cntnt){
      $("#expand_wrapper_dir").html(cntnt);
    }
    });
    return false;
});

$(".alias-dir-a").click(function(){
    var useremail=$("#useremail").val();
    var alias=$(this).html();
    if(alias==="+")
    {
        $.ajax({
            dataType: "html",
            type: "POST",
            url: "direcnew.php",
            data: "email="+useremail,
            success: function(cntnt){
                $("#direc-content").html(cntnt);
            }
        });
        return false;
    }
});

Ajax loaded file (direcalias.php)

<?php
    session_start();
    header("Content-Type: text/html; charset=ISO-8859-1");
    include_once 'connection.php';
    $conn = bbddconnect();
    $email=$_SESSION['useremail'];
    $query = "SELECT CODIDIR,ALIAS"
            . " FROM CLIENTE C,DIRECCION D"
            . " WHERE EMAIL LIKE '$email' AND C.CODICNT = D.CODICNT;"; 

    $result3 = mysqli_query($conn,$query)or die(mysqli_error());
    $recount = mysqli_num_rows($result3);
    echo '<div class="menu-alias madir">';
        for($i=0;$i<$recount;$i++)
        {
        $row = mysqli_fetch_array($result3);

    echo '<div class="alias adir ali-'.$i.'">
        <a href="javascript:void(0)" class="alias-dir-a">'.$row['ALIAS'].'</a>
    </div>';

        }

    echo '<div class="alias adir ali-nuevo">
        <a href="javascript:void(0)" class="alias-dir-a">+</a>
    </div>
    </div>
    <div id="direc-content"></div>';

    //echo '<script>'.
    //            'var url = "'.$_SESSION['url'].'js/direcinfo.js";'.
    //            '$.getScript(url);'.
    //    '</script>';

?>

The problem I have is that when the direcalias.php file is called I need to call the js file again (edited part) because if I don't, it does not recognize when I click on $(".alias-dir-a").click(function()). Is what I want to do possible?

Upvotes: 1

Views: 1442

Answers (1)

Supun Praneeth
Supun Praneeth

Reputation: 3150

It beacuse you call .alias-dir-a before it create. you can use on method

$(document).on("click", ".alias-dir-a", function() {

    var useremail=$("#useremail").val();
    var alias=$(this).html();
    if(alias==="+")
    {
        $.ajax({
            dataType: "html",
            type: "POST",
            url: "direcnew.php",
            data: "email="+useremail,
            success: function(cntnt){
                $("#direc-content").html(cntnt);
            }
        });
        return false;
    }
});

Upvotes: 1

Related Questions