/    Sign up×
Community /Pin to ProfileBookmark

can i use php within JS?

can i use php script to get response from database within a JS function?
function js()
{
<?php
$host=”localhost”; // Host name
$username=”root”; // Mysql username
$password=””; // Mysql password
$db_name=”test”; // Database name
$tbl_name=”register”; // Table name

// Connect to server and select databse.
mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect”);
mysql_select_db(“$db_name”)or die(“cannot select DB”);

// username and password sent from signup form
$username=$_GET[‘username’];
$sql=”SELECT * FROM $tbl_name WHERE username=’$username'”;
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count!=0)
{
verifyuname=0;
die();
}
?>
}
thnx in advance?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@bosky101Mar 03.2007 — can i use php script to get response from database within a JS function?

function js()

{

<?php

.

.

if($count!=0)

{

echo "var verifyuname=0;";

die();

}else{

echo "var verifyuname= '$username';";

}

.

.

?>

}
[/QUOTE]

this might work ,but as your funtionality grows, it might be tedious to maintaint this design . try going obect oriented .keep your login check the beginning of all pages through a [b]require_once("do_login_check"); [/b] and then put that info into easily accessable variables.

basically try to mix php and js code only to the minimal level for better readability.
Copy linkTweet thisAlerts:
@konithomimoMar 03.2007 — can i use php script to get response from database within a JS function?

function js()

{

<?php

$host="localhost"; // Host name

$username="root"; // Mysql username

$password=""; // Mysql password

$db_name="test"; // Database name

$tbl_name="register"; // Table name

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from signup form

$username=$_GET['username'];

$sql="SELECT * FROM $tbl_name WHERE username='$username'";

$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count!=0)

{

verifyuname=0;

die();

}

?>

}

thnx in advance?[/QUOTE]


yes you can use PHP in JS as you are doing, but in order for JS to process the data you must return it via an echo/print command, as bosky illustrated.
×

Success!

Help @asmitaisit 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.27,
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,
)...