How would I intergrate Javascript with php

SpaceshipSpaceship
Watch

Ray

VIP Member
Impact
167
how would i intergrate javascript into the php brackets, i cant get it to work right, even when i change the "'s to \".

can some one please help me
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
you just need to use a ' to wrap the javascript and \' to hide markup script. Give me an example and ill take a shot

-Steve
 
0
•••
#connect to MySQL
$conn = @mysql_connect("localhost","---","-----")
or die("Could not connect to MySQL");
#select a database
$db = @mysql_select_db("template_users",$conn)
or die("Could not select database");

#create the SQL query
$sql = "insert into pm (whoto,whofrom,subject,message)
values (\"$whoto\",\"$username\",\"$sub\",\"$msg\")";
$result = @mysql_query($sql,$conn)
or die("Could not execute query");


if($result)
{

<script type="text/javascript">

<!--
function delayer(){
document.location = "members.php?page=messagebox"
}
-->
</script>


}else{
echo("failed");

?>
 
Last edited:
0
•••
ok, you're problem is simple,you just forgot a couple lines of code - try this

PHP:
<?php
#connect to MySQL
$conn = @mysql_connect("localhost","---","-----")
or die("Could not connect to MySQL");
#select a database
$db = @mysql_select_db("template_users",$conn)
or die("Could not select database");

#create the SQL query
$sql = "insert into pm (whoto,whofrom,subject,message)
values (\"$whoto\",\"$username\",\"$sub\",\"$msg\")";
$result = @mysql_query($sql,$conn)
or die("Could not execute query");


if($result)
{
?>
<script type="text/javascript">

<!--
function delayer(){
document.location = 'members.php?page=messagebox';
}
-->
</script>
<?
}

}else{
echo("failed");

?>

let me know if that works

-Steve
 
0
•••
ok, this is what i have now, but i get an error. Parse error: parse error, unexpected $ in /home/template/public_html/mb/compose.php on line 69

theres no line 69 in the code... in only goest down to 68

#connect to MySQL
$conn = @mysql_connect("localhost","---","----")
or die("Could not connect to MySQL");
#select a database
$db = @mysql_select_db("----",$conn)
or die("Could not select database");

#create the SQL query
$sql = "insert into pm (whoto,whofrom,subject,message)
values (\"$whoto\",\"$username\",\"$sub\",\"$msg\")";
$result = @mysql_query($sql,$conn)
or die("Could not execute query");

if($result)
{
?>
<script type="text/javascript">

<!--
function delayer(){
document.location = 'members.php?page=messagebox';
}
-->
</script>


<?
}else{
echo("failed");
}
?>
 
0
•••
Can you paste the full code? :)
 
0
•••
ok i fixed ther error, the problem was that i had put an else command in wrong. Anyway, did what you told me, except it does the same thing, I included a a screen shot. Also the javascript still dont work

The code im using is below


PHP:
 if($result)
{
 ?>
 <script type="text/javascript">

<!--
function delayer(){
document.location = 'members.php?page=messagebox';
}
//-->
</script>
<body onLoad="setTimeout('delayer()', 5000)">
 <?

echo("<font face=\"Verdana\" size=\"2\" color=\"#333333\">Your message has been sent.</font><p><font face=\"Verdana\" size=\"2\" color=\"#333333\">You are now being redirected to your<a href=\"members.php?page=messagebox\"><font color=\"#333333\">message box</font></a>.</font></p>");


 }
}
?>


Screen Shot of problem
 
Last edited:
0
•••
instead of the javascript, you should use:
PHP:
<?php
header("Location: http://www.address.ext/members.php?page=messagebox");
?>
and keep everything in php - unless you have a valid reason for using js

-Steve
 
0
•••
well, the reason i use java for that is that i cant figure out how to have a timed redirection in php. Also my other reason for using java is googles adwords...
 
0
•••
could use sleep()
 
0
•••
if you use the php redirection, you can only use it if there is no output, otherwise you will get an error say the headers have already been sent out.
 
0
•••
thats not always true. The ob_start() command will buffer the headers so headers can be sent. Im great with php, just havin issues on things i never did.

more about output buffering
http://us4.php.net/ob_start
 
0
•••
PHP:
<?
if($result) { 
?> 
		<script type="text/javascript"> 
		<!-- 
		function delayer(){ 
		document.location = 'members.php?page=messagebox'; 
		} 
		//--> 
		</script> 
		<body onLoad="setTimeout('delayer()', 5000)"> 
		<? 
		echo ("<font face=\"Verdana\" size=\"2\" color=\"#333333\">Your message has been sent.</font><p><font face=\"Verdana\" size=\"2\" color=\"#333333\">You are now being redirected to your <a href=\"members.php?page=messagebox\"><font color=\"#333333\">message box</font></a>.</font></p>"); 
		//here print the rest of your html code
		//i suppose :
		echo ("</body>\n</html>");
		//these 2 lines help u a lot
		exit;
		die;
	}
} //we think that u open a condition before 'if($result) { '
?>

or you can write this (it's better for browser don't support javascript and more security)

PHP:
<? @session_start();

//---- your code

	if($result) { 
		$_SESSION['page']='messagebox';
		print "\n<meta http-equiv=\"refresh\" content=\"1; URL=members.php\">\n</head>\n<body>\n<br><br><br><font face=\"Verdana\" size=\"2\" color=\"#333333\">Your message has been sent.</font><p><font face=\"Verdana\" size=\"2\" color=\"#333333\">You are now being redirected to your <a href=\"members.php\"><font color=\"#333333\">message box</font></a>.</font></p><br>If nothing happens, <a href=\"index\">click here</a>.<br><br><br><br><br>";
		//here print the rest of your html code
		//i suppose :
		print "</body>\n</html>";
		//these 2 lines help u a lot
		exit;
		die;
	}
} //we think that u open a condition before 'if($result) { '
?>

and in members.php u can put something like :

PHP:
<? @session_start();
	if ( (isset($_SESSION['page'])) && ($_SESSION['page']=='messagebox') ) { 
		//content of members.php?page=messagebox
	}	
?>

Regards
 
0
•••
Dynadot — .com TransferDynadot — .com Transfer
Appraise.net

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back