Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Not sue why my text disappears - JS and php

    Hi, I am making a website that uses javascript to call a php file which in turn retrieves information from a MySQL database. everything works as expect and the information from the database appears momentarily but then it changes back to the original text.

    Any help much appreciated.
    Thanks Alex

    html / javascript file
    <html>
    <head><script>

    function showQ()
    {

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
    }
    xmlhttp.open("GET","getuser.php",true);
    xmlhttp.send();
    }
    </script>
    </head>
    <body>
    <form>
    <button onclick="showQ()">Try it</button>

    </form>
    <br>
    <div id="txtHint"><b>Question here</b></div>
    //this is the text that keeps coming back after showing the db data momentarily
    </body>
    </html>

    getuser.php PHP file
    <?php
    $con=mysqli_connect("localhost","275086","*******","275086");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM Questions WHERE QID = '1'");

    while($row = mysqli_fetch_array($result))
    {
    echo $row['Question'] . " " . $row['Marks'];
    echo "<br />";
    echo $row['Answer'];
    echo "<br />";
    }

    mysqli_close($con);
    ?>

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    after the ajax runs
    the text is changed
    to the responseText,
    then the form submits
    and that reloads
    the page
    leave off the form tags or
    do this
    <form onsubmit="return false">

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Alternatively add type="button" to the button tag to override the default type of submit.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •