www.webdeveloper.com
Results 1 to 2 of 2

Thread: Ajax onClick and PHP function?

  1. #1
    Join Date
    Nov 2009
    Posts
    1

    Ajax onClick and PHP function?

    Firstly i'd like to say that i don't know if i'm supposed to post this in the PHP section instead but anyway.

    Heres the deal, i have a login/register etc functions and i want to use Ajax to "Iframe" one part of the page (the content section) and the thing is that when i click on the button with the onClick function it doesn't remember anything of the form i just filled...

    Is there a way to create a submit function for onClick (because i don't want to get redirected to the actual registration)

    This is the Ajax Function:
    Code:
    	var klient;
    	function ajax(filurl) {
    	 klient = new XMLHttpRequest();
    	 klient.onreadystatechange = hanterare;
    	 klient.open("GET", filurl);
    	 klient.send(null);
    	}
    	function hanterare() {
    	document.getElementById("utdata").innerHTML= klient.responseText;
    	}
    This is the registration form:
    HTML Code:
    	<form action="register_ac.php" method="post">
    	Username: <input type="text" name="username" size="20">
    	Password: <input type="password" name="password" size="20">
    	Email: 	  <input type="text" name="email" size="20">
    	<input type="button" onClick="ajax('register_ac.php')" value="Register">
    	</form>
    And this is the register_ac.php:
    PHP Code:
    <?php 

    include("config.php"); 

    // connect to the mysql server
    $link mysql_connect($server$db_user$db_pass)
    or die (
    "Could not connect to mysql because ".mysql_error());

    // select the database
    mysql_select_db($database)
    or die (
    "Could not select database because ".mysql_error());

    // check if the username is taken
    $check "select id from $table where username = '".$_POST['username']."';"
    $qry mysql_query($check) or die ("Could not match data because ".mysql_error());
    $num_rows mysql_num_rows($qry); 
    if (
    $num_rows != 0) { 
    echo 
    "Sorry, the username $username is already taken.<br>";
    echo 
    "<a href=index.php>Try again</a>";
    exit; 
    } else {

    // insert the data
    $insert mysql_query("insert into $table values ('NULL', '".$_POST['username']."', '".$_POST['email']."', '".$_POST['password']."')")
    or die(
    "Could not insert data because ".mysql_error());

    // print a success message
    echo "Your user account has been created!<br>"
    echo 
    "Now you can <a href=index.php>Log in</a>"
    }

    ?>
    Any suggestions?
    If you need more just tell me...

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Do you mean: if the php echoes "Your user account has been created!" the button
    Code:
    <input type="button" onClick="ajax('register_ac.php')" value="Register">
    should change his behavior (and probably the value into "Log In" instead of "Register"), in order to submit the form to the index.php (instead the AJAX to register_ac.php)?

    Yes, it is possible, if this is what you want.
    Last edited by Kor; 11-27-2009 at 09:30 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles