/    Sign up×
Community /Pin to ProfileBookmark

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;
}[/CODE]

This is the registration form:

[code=html] <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>[/code]

And this is the register_ac.php:

[code=php]<?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>”;
}

?>[/code]

Any suggestions?
If you need more just tell me…

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@KorNov 27.2009 — Do you mean: if the php echoes "Your user account has been created!" the button
<i>
</i>&lt;input type="button" onClick="ajax('register_ac.php')" value="Register"&gt;

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.
×

Success!

Help @snebulan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...