0

I am trying to build a web app so i want to pass values from a php file to an html file with jquery $.get function.

Although $.get("url/file.php") works fine,the internal function of $.get("url/file.php",function(data){ alert("test123");}); doesn't work.

I found a sample script from w3schools and it works fine, I tried to replace the sample's file with my own file (url/file.php) and the internal function won't work, I also tried to replace my file with the sample file in my script and again the internal function won't work.

EDIT: Here is my code.What i am trying to do is to send username,password,email to the php file by clicking the signup button in order to register the user in the database and get back a string from the php file

The inde6.html file that contains the javascript is:

 <html>
      <head>
        <title>Array Review</title>
    <meta http-equiv="Content-Type" content="text/html; 
    charset=UTF-8" />
     <link href="css/index2.css" rel="stylesheet"  type="text/css">
     <script type="text/javascript" src="js/jquery.mobile-1.3.1.min.js"></script>
     <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <style type="text/css">
    p {display:none}
    input[type=text],input[type=password] {padding:5px; border:2px solid #ccc; 
    -webkit-border-radius: 5px;
    border-radius: 5px;
    }
    input[type=text],input[type=password]:focus {border-color:#333; }

    input[type=submit] {padding:5px 15px; background:#5A57B2; border:0 none;
    cursor:pointer;
    -webkit-border-radius: 5px;
    border-radius: 5px; }
    .stoixisi {
    color:white;
    }
    #eikona {
     background-image: url("owl.png");
     background-repeat: no-repeat;
    background-position: center top;
    height:11.28%;
        width:99%;    
    }
    body {
     height:470px;
        width:330px;
        background-image:url("bi2.png");
        background-repeat:repeat;
    }
    #whole,#page1 {
    height:100%;
    width:100%;
    }
    #demo {
    margin-left:6%;
    margin-top:1%;
    color:#FCE94D;
    }
    #demo1 {
    margin-left:6%;
    margin-top:0%;
    color:#FCE94D;
    }

    #form1 {
    margin-top:-2%;
    height:42.55%;
    width:75.76%;
    }
    #form2 {
    margin-top:2%;
    height:30%;
    width:75.76%;
    }
    </style>
    <script>
    window.onload= function () {
    document.getElementById("ii").style.height=window.innerHeight+"px";
    document.getElementById("ii").style.width=window.innerWidth+"px";
    document.getElementById("whole").style.height=window.innerHeight+"px";
    document.getElementById("whole").style.width=window.innerWidth+"px";
    };
     $(document).ready(function(){
      $("#signupp").click(function(){
    var url="http://giannisgoulias.co.nf/Connect/welcome.php?"+"name="+$("#name").val()+"&password="+$("#password").val()+"&email="+$("#email").val();

    $.get(url,function(data){alert('test');});
        });
      });

    </script>
      </head>
      <body id="ii">
     <div data-role="page" data-control-title="Home" id="page1">
    <div id="whole">
    <div id="eikona"></div>
    <h2 id="demo1">Hi!Please Sign Up!</h2>
        <p>

    <form action="inde6.html" method="post" id="form1">
     <div class="stoixisi">Username:</div> <input type="text" name="name" id="name"><br>
    <div class="stoixisi">Password:</div> <input type="password" name="password" id="password"><br>
    <div class="stoixisi">E-mail:</div> <input type="text" name="email" id="email"><br>
    <input type="Submit" value="Sign Up" id="signupp">
    </form>
    <h2 id="demo1">Sign In!</h2>
    <form action="signin.php" method="post" id="form2">
    <div class="stoixisi">Username:</div> <input type="text" name="name"><br>
    <div class="stoixisi">Password:</div> <input type="password" name="password"><br>
    <input type="Submit" value="Sign In" onclick="signin()">
    </div>
    </form>    
        </p>
      </body>
    </html>

AND the php file is

  <html>
    <head></head>
    <body>
    <p>
     <?php 


    $dbhost=****;
    $dbuser=*****;
    $dbpass=*****;
    $db=*****;

    $conn= mysql_connect($dbhost,$dbuser,$dbpass);
    mysql_select_db($db);

    mysql_query ("set character_set_results='utf8'");

    $nname = mysql_real_escape_string($_GET["name"]);
    $ppassword = mysql_real_escape_string($_GET["password"]);
    $mmail=mysql_real_escape_string($_GET["email"]);
    $query2="SELECT User_Name FROM table1 WHERE User_Name IN ('$nname')";
    $query3="SELECT User_Mail FROM table1 WHERE User_Mail IN ('$mmail')";
    $result2=mysql_query($query2);
    $result3=mysql_query($query3);
    $num_rows = mysql_num_rows($result2);
    $num_rows2 = mysql_num_rows($result3);

    $user_exists=0;
    $mail_exists=0;
    if ($num_rows>0 ) {
    $user_exists=1;
    }
    if ($num_rows2>0) {

    $mail_exists=1;
    }

    if ($user_exists==0 && $mail_exists==0) 
    {

    $query1="INSERT INTO table1 
    (User_Name,User_Password,User_Mail) VALUES ('$nname','$ppassword','$mmail')";

    $result1=mysql_query($query1);

    $queryy="INSERT INTO table2 
    (UserName) VALUES ('$mmail')";

    $resultt=mysql_query($queryy);


    $queryyl="INSERT INTO NGames 
    (player,gnumber) VALUES ('$mmail',0)";

    $resulttl=mysql_query($queryyl);


    }


    if ($user_exists==1 && $mail_exists==1) {
    $out="Username and mail already exists!";

    }
    else if ($user_exists==1) {
      $out= "This username already exists!";
                }
    else
    { 
    if ($mail_exists==1) {
      $out="This mail already exists!";
                }
     else {
      $out="Sign in to play!";
               };
               }

    echo $out;


    ?>
    </p>
    </body>
    </html> 
Giannis
  • 11
  • 2
  • The problem is in the php file - visit it directly in your browser and see what it outputs – Steve Nov 11 '15 at 21:01
  • are you outputting or echoing out anything from your PHP file? the variable `data` in `function(data)` will only hold a value if PHP sends an output. – CodeGodie Nov 11 '15 at 21:10
  • You're going to need to post more of your code so that we can see whats causing the issue. – CodeGodie Nov 11 '15 at 21:12
  • or you don't have the right path. Inspect the actual request in browser dev tools network – charlietfl Nov 11 '15 at 21:17
  • @Steve I visited directly my browser and saw the string i expected. – Giannis Nov 11 '15 at 21:20
  • OK, so are you seeing errors in the javascript console? What are you seeing in the network tab? – Steve Nov 11 '15 at 21:23
  • @Steve the status from the php file is "pending" – Giannis Nov 11 '15 at 21:34
  • weird, never seen that. Do you have adblock installed in chrome? :http://stackoverflow.com/questions/10763754/chrome-xmlhttprequest-hanging – Steve Nov 11 '15 at 21:46
  • @Steve I uploaded my code! – Giannis Nov 11 '15 at 22:30
  • I genuinely dont see anything wrong with the ajax (the php is indeed messy, but isnt throwing any errors, so isnt the issue here). – Steve Nov 11 '15 at 22:46
  • @Steve But why does the php file causes a problem even with a sample html file? – Giannis Nov 11 '15 at 22:54
  • I really dont know - as i said, it seems fine to me. Im kind of baffled – Steve Nov 11 '15 at 22:55
  • @Steve http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_ajax_get i replace the "demo_test.asp" with "http://giannisgoulias.co.nf/Connect/welcome.php?name=iakovos" and still doesn't work.Anyway,if the code is fine maybe the hosting site has something to do with it or my adblock (which i have turned off) – Giannis Nov 11 '15 at 22:59
  • It will fail if you use the w3schools page because of CORS - you cannot make an ajax request to page on a different domain unless the page sends a Access-Control-Allow-Origin header. Is your ajax also being made from another domain? – Steve Nov 11 '15 at 23:06
  • Yes,that's it! When the file is in the same domain it works! For now i'll upload all my files to that domain but when i'm done i'll have to put that header! Thanks!! :) – Giannis Nov 11 '15 at 23:13
  • Great, we got there in the end! Glad I could help you. – Steve Nov 11 '15 at 23:30

0 Answers0