I am writing the following code:
<?php
require_once("xajax/xajax_core/xajaxAIO.inc.php");
$xajax = new xajax();
function testEE($datos){
$x = $datos;
$rr = new xajaxResponse();
$rr->alert($x);
return $rr;
}
$xajax->registerFunction("testEE");
$xajax->processRequest();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test E</title>
<?php $xajax->printJavascript("xajax/"); ?>
</head>
<body>
<div id="display" style="width:750px; margin-right:auto; margin-left:auto; margin-top:100px;">
<form id='forma' name='forma'>
<table cellpadding='8' style='float:left;'>
<tr><td>Pregunta:</td><td><textarea name='question1' id='question1' cols='40' rows='8'></textarea></td></tr>
<tr><td colspan='2'><input type='button' value='Agregar' onclick="xajax_testEE(document.getElementById('question1').value)"></td></tr>
</table>
</form>
</div>
</body>
</html>
It works fine if a put a single word in the textarea but if I try to put 2 or more words it does nothing
I have tried this:
<?php
require_once("xajax/xajax_core/xajaxAIO.inc.php");
$xajax = new xajax();
function testEE($datos){
$x = $datos;
$rr = new xajaxResponse();
$rr->alert($x);
return $rr;
}
$xajax->registerFunction("testEE");
$xajax->processRequest();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test E</title>
<?php $xajax->printJavascript("xajax/"); ?>
</head>
<body>
<div id="contenido">
<div id="display" style="width:750px; margin-right:auto; margin-left:auto; margin-top:100px;">
<form id='forma' name='forma'>
<table cellpadding='8' style='float:left;'>
<tr><td>Pregunta:</td><td><textarea name='question1' id='question1' cols='40' rows='8'></textarea></td></tr>
<tr><td>Respuesta 1:</td><td><textarea name='respuesta1' id='respuesta1' cols='40' rows='8'></textarea></td></tr>
<tr><td colspan='2'><input type='button' value='Agregar' onclick="myFunction()"></td></tr>
</table>
</form>
</div>
</div>
<script>
function myFunction() {
var x = document.getElementById("question1").value;
xajax_testEE(x)
alert('myFunction alert': x)
}
</script>
</body>
</html>
When I enter a single word, I get the 2 alerts (as expected) but if I enter more than 1 word I only get the alert of myFunction with the complete string but I don't get the alert id the xajax function
Related
I am working on a little forum, and I want it to be easy to identify a staff member.
A the moment, a user is defined as an $admin in the config file:
<?php
/******************************************************
------------------Required Configuration---------------
Please edit the following variables so the forum can
work correctly.
******************************************************/
//We log to the DataBase
mysql_connect('127.0.0.1', 'root', '');
mysql_select_db('forum_database');
//Forum Staff
$admin='The_Darthonian'; // For admin forum features
/******************************************************
-----------------Optional Configuration----------------
******************************************************/
//Forum Home Page
$url_home = 'index.php';
//Design Name
$design = 'default';
/******************************************************
----------------------Initialization-------------------
******************************************************/
include('init.php');
?>
I have an icon at the path of default/images/role_moderator.gif that I want to appear on a profile if a user is defined as an admin
I further have a userid variable. For example, the first account would be 1, then the second 2 and so on which are unique. Below is the profile code:
<?php
//This page display the profile of an user
include('config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="<?php echo $design; ?>/style.css" rel="stylesheet" title="Style" />
<title>User Profile</title>
</head>
<body>
<div class="header">
<img src="<?php echo $design; ?>/images/logo.png" alt="Forum" />
</div>
<div class="content">
<?php
if(isset($_SESSION['username']))
{
$nb_new_pm = mysql_fetch_array(mysql_query('select count(*) as nb_new_pm from pm where ((user1="'.$_SESSION['userid'].'" and user1read="no") or (user2="'.$_SESSION['userid'].'" and user2read="no")) and id2="1"'));
$nb_new_pm = $nb_new_pm['nb_new_pm'];
?>
<div class="box">
<div class="box_left">
Home > Users > Profile
</div>
<div class="box_right">
Your messages(<?php echo $nb_new_pm; ?>) - <?php echo htmlentities($_SESSION['username'], ENT_QUOTES, 'UTF-8'); ?> (Logout)
</div>
<div class="clean"></div>
</div>
<?php
}
else
{
?>
<div class="box">
<div class="box_left">
Home > Users > Profile
</div>
<div class="box_right">
Sign Up - Login
</div>
<div class="clean"></div>
</div>
<?php
}
if(isset($_GET['id']))
{
$id = intval($_GET['id']);
$dn = mysql_query('select username, email, avatar, signup_date from users where id="'.$id.'"');
if(mysql_num_rows($dn)>0)
{
$dnn = mysql_fetch_array($dn);
?>
This is the profile of "<?php echo htmlentities($dnn['username']); ?>" :
<?php
if($_SESSION['userid']==$id)
{
?>
<br /><div class="center">Edit my profile</div>
<?php
}
?>
<table style="width:500px;">
<tr>
<td><?php
if($dnn['avatar']!='')
{
echo '<img src="'.htmlentities($dnn['avatar'], ENT_QUOTES, 'UTF-8').'" alt="Avatar" style="max-width:100px;max-height:100px;" />';
}
else
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
</head>
<body>
<?php
?>
<div>
<img src="default/images/no_avatar.jpg" alt="no_avatar" />
</div>
</body>
</html>
<?php
}
?></td>
<td class="left"><h1><?php echo htmlentities($dnn['username'], ENT_QUOTES, 'UTF-8'); ?></h1>
This user joined the website on <?php echo date('Y/m/d',$dnn['signup_date']); ?></td>
</tr>
</table>
<?php
if(isset($_SESSION['username']) and $_SESSION['username']!=$dnn['username'])
{
?>
<br />Message "<?php echo htmlentities($dnn['username'], ENT_QUOTES, 'UTF-8'); ?>"
<?php
}
}
else
{
echo 'We could not find this user anywhere. Prehaps their account was removed.';
}
}
else
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
</head>
<body>
<?php
?>
<div>
<img src="/default/images/no_avatar.jpg" alt="no_avatar" />
</div>
</body>
</html>
<?php
}
?>
</div>
<div class="foot">About Us - Terms of Service</div>
</body>
</html>
How can I make it so that if a user is defined as an $admin in the config, they have the icon appear on their profile?
I am learning web development from "PHP and MongoDB Web Development" book. I came across these instructions for creating a blog and adding comments into it.
1)Open blog.php in your text editor and replace the existing code in it with the
following:
<?php
$id = $_GET['id'];
try {
$connection = new Mongo();
$database
= $connection->selectDB('myblogsite');
$collection = $database->selectCollection('articles');
} catch(MongoConnectionException $e) {
die("Failed to connect to database ".$e->getMessage());
}
$article = $collection->findOne(array('_id' =>
new MongoId($id)));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
<link rel="stylesheet" href="style.css" />
<title>My Blog Site</title>
</head>
<body>
<div id="contentarea">
<div id="innercontentarea">
<h1><?php echo $article['title']; ?></h1>
<p><?php echo $article['content']; ?></p>
<div id="comment-section">
<h3>Comments</h3>
<?php if (!empty($article['comments'])): ?>
<h3>Comments</h3>
<?php foreach($article['comments'] as $comment):echo $comment['name'].' says...';?>
<p><?php echo $comment['comment']; ?></p>
<span>
<?php echo date('g:i a, F j', $comment['posted_at']->sec); ?>
</span><br/><br/><br/>
<?php endforeach;endif;?>
<h3>Post your comment</h3>
<form action="comment.php" method="post">
<span class="input-label">Name</span>
<input type="text" name="commenter_name" class="comment-input"/>
<br/><br/>
<span class="input-label">Email</span>
<input type="text" name="commenter_email" class="comment-input"/>
<br/><br/>
<textarea name="comment"vrows="5"></textarea><br/><br/>
<input type="hidden" name="article_id" value="<?php echo $article['_id']; ?>"/>
<input type="submit" name="btn_submit" value="Save"/>
</form>
</div>
</div>
</div>
</body>
</html>
2) Create comment.php file with the following code:
<?php
$id = $_POST['article_id'];
try {
$mongodb = new Mongo();
$collection = $mongodb->myblogsite->articles;
} catch (MongoConnectionException $e) {
die('Failed to connect to MongoDB '.$e->getMessage());
}
$article = $collection->findOne(array('_id' => MongoId($id)));
$comment = array('name' => $_POST['commenter_name'],'email' => $_POST['commenter_email'],'comment' => $_POST['comment'],'posted_at' => new MongoDate());
$collection->update(array('_id' => new MongoId($id)),array('push' => array('comments' => $comments)));
header('Location: blogs.php?id='.$id);
?>
3)Navigate to blogs.php in your browser, click on the Read More link of the top
article to read its full content in the blog.php page. The code for blogs.php is as follows:
<?php
try {
$connection = new Mongo();
$database
= $connection->selectDB('myblogsite');
$collection = $database->selectCollection('articles');
} catch(MongoConnectionException $e) {
die("Failed to connect to database ".$e->getMessage());
}
$cursor = $collection->find();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
<link rel="stylesheet" href="style.css" />
<title>My Blog Site</title>
</head>
<body>
<div id="contentarea">
<div id="innercontentarea">
<h1>My Blogs</h1>
<?php while ($cursor->hasNext()):
$article = $cursor->getNext(); ?>
<h2><?php echo $article['title']; ?></h2>
<p>
<?php echo substr($article['content'], 0,
200).'...'; ?>
</p>
<a href="blog.php?id=<?php echo $article['_id'];
?>">Read more</a>
<?php endwhile; ?>
</div>
</div>
</body>
</html>
When I try to post a comment it is redirecting to a blank page. How to make this code work?
Above all code in comment.php put the following (just under the PHP opening tag):
error_reporting(E_ALL);
ini_set('display_errors', 'On');
That will show you the error that's causing the blank page and will hopefully help you on your way.
i have try this code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php phpinfo(); die; ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>TRY IT</title>
</head>
<body>
<?php
echo "test";
?>
</body>
</html>
It is very simple echo syntax in php. Output that should i got is string "test" on my screen. but output that i got just a blank page.
For your information php version that installed on my computer is version 5.3.8
anyone know what happen?
Remove the die on the second line.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php phpinfo(); die; ?>
^^^^ Remove this
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>TRY IT</title>
</head>
<body>
<?php
echo "test";
?>
</body>
</html>
I have this HTML that works very well.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script language="JavaScript"><!--
function refreshIt() {
if (!document.images) return;
var myImage = document.getElementById('signals');
myImage.src = 'http://lessthanrandom.com/screencast/signals.jpg?' + Math.random();
setTimeout('refreshIt()', 900000); // refresh every 60 seconds
//setTimeout('refreshIt()', 1800000); // refresh every 1/2 hour
}
//--></script>
<style>
*{margin:0;padding:0}
html, body {height:100%;width:100%;overflow:hidden}
table {height:100%;width:100%;table-layout:static;border-collapse:collapse}
iframe {height:100%;width:100%}
.header {border-bottom:1px solid #000}
.content {height:100%}
</style>
</head>
<body onLoad="setTimeout('refreshIt()', 900000)">
<table>
<tr><td class="content">
<img src="http://lessthanrandom.com/screencast/signals.jpg" width="100%" id="signals" name="signals"></td></tr>
</table>
</body>
</html>
If I add the following PHP above this and change to a PHP file, the image displays, but does not refresh. I'm new to PHP and Javascript.
<?php
include_once "/home/lesadmin/public_html/wp-load.php";
if (current_user_can("access_s2member_level1"))
{
}
else
exit("Must have a valid membership to view this content.");
?>
is it possible to embed this into html
if (empty($_POST['extras'])) {
$message .="<br /> No extras selected <br />";
} else {
foreach($_POST['extras'] as $extra)
{
$message .="<br />Extras: ".$extra."";
}
}
I would like to place the above php statement at the bottom of this html code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Booking System</title>
<link rel="stylesheet" href="css/bs-admin.css" type="text/css" />
</head>
<body>
<noscript>
<div class="js_error">Please enable JavaScript or upgrade to better browser</div>
</noscript>
<div id="index">
<h1>Thank you for your reservation!</h1>
<p>
<h3>Your Booking is as follows:</h3>
<p>Dear <b><?php echo $custInf[0] ?></b>,
<p>You have Booked: <?php echo $eventInf[0] ?>
<p>Booking Date: <?php echo $eventInf[2] ?>
<p>Booking descriptiong: <?php echo $eventInf[1] ?>
<p>Number of machines booked: <?php echo $qty ?>
<p>Street: <?php echo $comments ?>
<p>Suburb: <?php echo $suburb ?>
<p>Postcode: <?php echo $postcode ?>
<p>Dropoff: <?php echo $dropoff ?>
<p>Duraton: <?php echo $duration ?>
If it's got php code in it then it's no more HTML.
You have to call it .php or .phtml.
PHP generates, or outputs html.
You can have pure html in .php scripts (outside the <?php ?> tags), but not the other way around (i.e. no php code in regular .html files).
If you want to add some logic (the PHP code) within it, you need to have it parsed by a webserver which will, in turn, generate html.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Booking System</title>
<link rel="stylesheet" href="css/bs-admin.css" type="text/css" />
</head>
<body>
<noscript>
<div class="js_error">Please enable JavaScript or upgrade to better browser</div>
</noscript>
<div id="index">
<h1>Thank you for your reservation!</h1>
<div>
<h3>Your Booking is as follows:</h3>
<p>Dear <b><?php echo $custInf[0]; ?></b>,</p>
<p>You have Booked: <?php echo $eventInf[0]; ?></p>
<p>Booking Date: <?php echo $eventInf[2]; ?></p>
<p>Booking descriptiong: <?php echo $eventInf[1]; ?></p>
<p>Number of machines booked: <?php echo $qty; ?></p>
<p>Street: <?php echo $comments; ?> </p>
<p>Suburb: <?php echo $suburb; ?></p>
<p>Postcode: <?php echo $postcode ?></p>
<p>Dropoff: <?php echo $dropoff; ?></p>
<p>Duraton: <?php echo $duration; ?></p>
</div>
<?php
$message = "";
if (empty($_POST['extras'])) $message .="<br /> No extras selected <br />";
else
{
foreach($_POST['extras'] as $extra)
{
$message .="<br />Extras: ".$extra;
}
}
echo $message;
?>
</div>
</body>
</html>
I think you're needing to create an empty variable $message before you could start appending "extras" to it. Then all you need to do is echo $message.
Yes you can, however that's not a good way to do it. You should go for the Model-View-Controller model. It separates the HTML code from the actual code that does processing. Many PHP Framework does this. (Though personally i find them too clunky and wrote my own)
Also, embedding HTML into PHP is bad, as again, code should be separated from the HTML by as much as possible.
Example of views and controllers (From my framework):
Controller:
class Controllers extends BaseController{
function index($args=array()){
// process data
$this->render('index', array('data1'=>$data));
}
}
View:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My site</title>
</head>
<body>
<h1><?php echo $page->data1; // echos out $data from the view ?></h1>
</body>
</html>
This is much cleaner than your model of embedding php into the HTML and/or vice versa.
Take a look at frameworks, they are usually pretty helpful, although PHP frameworks are generally very restrictive as to what you can do.
Yes, you can do what you are asking. Make sure the extension is recognizable by the php interpreter (usually .php)
If you need to hack something up quick, this is ok. But for anything else than that, look into using some sort of templating language. This becomes an important point because you want to seperate your logic from your display for the sanity of yourself and other developers that will work on your code in the future.
edit: oh, also very important. Don't use $_POST this way without sanitizing the data. It's ripe for XSS injections.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Booking System</title>
<link rel="stylesheet" href="css/bs-admin.css" type="text/css" />
</head>
<body>
<noscript>
<div class="js_error">Please enable JavaScript or upgrade to better browser</div>
</noscript>
<div id="index">
<h1>Thank you for your reservation!</h1>
<!--- bunch of stuff omitted here -->
<?php
if (empty($_POST['extras'])) {
echo "<br /> No extras selected <br />\n";
} else {
foreach($_POST['extras'] as $extra)
{
echo "<br />Extras: ".$extra."\n";
}
}
?>