Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthorFelony
    • CommentTimeMar 28th 2009
     permalink
    Hi, I'm starting to get mad with these codes.

    The thing is, I've done a code that adds data into a mysql database, but I don't want to reload the page.

    This is my HTML page (there's extra code but only for decorating):
    ---------------------------
    <script src="saving.js"></script>
    ...
    <form method="POST"><input type="hidden" name="phpMyAdmin" value="4594f30712f4fabaff6997416810f3f2" />
    <table width="350">
    <tr><td><b>First Name:</b></td><td><input type="text" name="fname" /></td></tr>
    <tr><td><b>Last Name:</b></td><td><input type="text" name="flast" /></td></tr>
    <tr><td><b>Phone Number:</b></td><td><input type="text" name="fphone" /></td></tr>
    <tr><td><b>Address:</b></td><td><input type="text" name="faddr" /></td></tr>
    <table>
    <br>
    <br>
    <input type="Submit" value="Submit" onClick="User(fname,flast,fphone,faddr);"/>
    </form>
    <p>
    <div id="txtHint"><b>Info here.</b></div>
    </p>
    --------------------------

    This is my JS code:
    --------------------------
    var xmlHttp;

    function User(str1,str2,str3,str4)
    {
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
    alert ("Browser does not support HTTP Request");
    return;
    }
    var url="insert.php";
    url=url+"?fname="+str1+"&flast="+str2+"&fphone="+str3+"&faddr="+str4;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    }

    function stateChanged()
    {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
    document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
    }
    }

    function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
    //Internet Explorer
    try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    }
    return xmlHttp;
    }
    -----------------------------------

    This is my PHP code:
    -----------------------------------
    <?php

    $fname = $_GET["fname"];
    $flast = $_GET["flast"];
    $fphone = $_GET["fphone"];
    $faddr = $_GET["faddr"];

    $dbhost = 'localhost:3306';
    $dbuser = '***********';
    $dbpass = '***********';

    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to MySQL!');
    $dbname = 'records';

    mysql_select_db($dbname);

    $query = "
    INSERT INTO registry (name, last, phone, address)
    VALUES ('$fname', '$flast', '$fphone', '$faddr')
    ";

    mysql_query($query) or die("Error, insert query failed");
    mysql_close($conn);

    echo "Data added succesfully!";

    ?>
    ---------------------------------------

    The deal is simple... is not working!

    I just want to add data to mysql with AJAX so the browser doesnt reload the page.

    Using MULTIPLE!!!!! PARAMETERS!!!!!, not just one as seen on w3schools example.

    Thanks in Advance!
  1.  permalink
    You'd probably get things done a lot easier if you used jQuery. This is all it would take:

    $.post("test.php", { first: "1", second: "2" },
    function(data){
    alert("Data Loaded: " + data);
    });


    This example would alert whatever was echoed at test.php. The $.post function sends the parameters "first" and "second" through POST with the values "1" and "2" respectively. Read more at the jQuery Docs page.
Add your comments
    Username Password
  • Format comments as (Help)