writing JavaScript with PHP - php

I am trying to right some some JavaScript to define a variable using PHP. The variable has a little jQuery in it which should get rendered by the client. I say "should" but really mean I want it to. What should I do to make o2 the same as o1 (i.e. covert the jQuery)?
Thank you!
<!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=ISO-8859-1" />
<title></title>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script>
$(function() {
var o1={"foo":{"bar": "abc/index.php?="+$("#id").val()}};
console.log(o1);
<?php
$d='abc/index.php';
$json='{"foo":{"bar": "'.$d.'?id=\"+$(\"#id\").val()"}}';
//echo($json);
$json=json_decode($json);
//echo(print_r($json,1));
$json=json_encode($json);
//echo($json);
echo("var o2=".$json.";");
?>
console.log(o2);
});
</script>
</head>
<body>
<input type="hidden" id="id" value="123" />
</body>
</html>

You have this:
<input type="hidden" id="id" value="123" />
So I can assume that you are getting that id from a db right? Maybe like this:
<input type="hidden" id="id" value="<?php echo $row[id];?>" />
If this is the case do the following (I tested and works):
You don't need hidden inputs to do it.
<!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=ISO-8859-1" />
<title></title>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script>
$(function() {
<?php
//From your SQL query you obtained
//$row['id'] = 123;
$id = $row['id'];
?>
var o1={"foo":{"bar": "abc/index.php?=" + <?php echo $id ?>}};
console.log(o1);
<?php
$d='abc/index.php';
$json='{"foo":{"bar": "'.$d.'?='.$id.'"}}';
//echo($json);
$json=json_decode($json);
//echo(print_r($json,1));
$json=json_encode($json);
//echo($json);
echo("var o2=".$json.';');
?>
console.log(o2);
});
</script>
</head>
<body>
</body>
</html>

Untested, may contain errors:
<script>
$(function() {
var o1={"foo":{"bar": "abc/index.php?="+$("#id").val()}};
console.log(o1);
<?php
$d='abc/index.php';
$json='{"foo":{"bar": "' . $d . '?="+$("#id").val()}}';
echo("var o2=".$json.';');
?>
console.log(o2);
});
</script>

I prefer make json with the json_encode, to avoid unexpected problems with parse and validation of json.
the json_encode and json_decode only work with UTF-8, another charset will result in a blank string, if it has special chars.
<script>
<?php
$d = "abc/index.php";
$json['foo']['bar'] = $d . '?id=$("#id").val()';
echo "var o2 = ".json_encode($json);
?>
console.log(o2);
</script>

Related

Using str_replace to replace HTML comments with PHP tags

Using PHP, I'm trying to create a function that will replace specific opening and closing HTML comment with opening and closing PHP tags.
What's happening is when I enter the commented HTML to be the subject string, it is being embedded into the HTML document rather then being replaced with PHP tags. However, when I enter something other than HTML comments it's fine - as you'd expect.
var_dump() returns string(32) "" and NULL for $foo and $bar respectively.
For example,the function below is intended to replace:
<!--CODE echo"hello world"; CODE--!>
With:
<?php echo"hello world"; ?>
The code:
<?php
function code($subject){
$php=array("<?php","?>"); //the replace string array
$html=array("<!--CODE","CODE--!>"); //the search string array
$subject=str_replace($html,$php,$subject); //search the subject and replace strings
}
if(isset($_POST['submit'])){
$foo=$_POST['foo'];
$bar=code($foo);
}
?>
<!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>Untitled Document</title>
</head>
<body>
<?php
var_dump($foo);
var_dump($bar);
?>
<form method="post" action="">
<input type="text" name="foo" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Any ideas? Would appreciate your input.
I hope this is what you tried to accomplish.
<?php
function code($subject) {
$php = array("<?php", "?>"); //the replace string array
$html = array("<!--CODE", "CODE-->"); //the search string array
return str_replace($html, $php, $subject); //search the subject and replace strings
}
if(isset($_POST['foo'])) {
$foo = $_POST['foo'];
$bar = code($foo);
}
?>
<!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>Untitled Document</title>
</head>
<body>
<?php
if (isset($foo)) {
echo 'foo = ';
var_dump($foo);
}
if (isset($bar)) {
echo 'bar = ';
var_dump($bar);
}
?>
<form method="post" action="">
<input type="text" name="foo" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
I edited the comment closing tag in you $html array with the HTML standard comment closing tag and edited the third row of your code() function. Now the values is not assigned back to $subject but is retured (return str_replace($html, $php, $subject)).

php reCAPTCHA not displaying

I am trying to add reCAPTCHA to my php/html site. But it does not display. I have downloaded the library and set up my site so I have the public and private keys. I have created a basic page to try and get this working but still no luck. This is my php 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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once('lib/recaptchalib.php');
?>
<title>Test odds and ends</title>
</head>
<body>
<form method="post" action="">
<?php
$publickey = "~hidden~";
echo recaptcha_get_html($publickey);
?>
</form>
</body>
</html>
The page displays completely blank. The html produced is:
<!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 odds and ends</title>
</head>
<body>
<form method="post" action="">
<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=~hidden~">
</script>
<noscript>
<iframe src="http://www.google.com/recaptcha/api/noscript?k=~hidden~" height="300" width="500" frameborder="0">
</iframe><br/>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
</noscript>
</form>
</body>
</html>
Does anyone know why this doesn't work? Many thanks in advance.

Pop up window closed without completion of the function

I made a pop-up window that has a submit form and upon submission it must close but i first want to process the information from the pop-up window in a different page without displaying that other page. How can I do it?
my popupwindow contains this 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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function closeSelf(){
self.close();
return true;
}
</script>
<title>Add Activity</title>
</head>
<body>
<form action="./addact.php" method="post" onsubmit ="return closeSelf()">
<table width="500" border="1"><br/>
<tr>
<td>Activity Name</td>
<td>Assigned Person</td>
<td>Deadline</td>
</tr>
<tr>
<td> <input name="activities" type="text" size="40%"/></td>
<td><input name="name" type="text" size="40%"/></td>
<td><input type="date" name="deadline" size="20%"/></td>
</tr>
</table>
<input type="submit" name = "saved" id="saved"/>
</form>
</body>
</html>
and my other page contains this
<?php session_start(); ?>
<!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>Untitled Document</title>
</head>
<?php
include('config.php');
$actname= $_POST['activities'];
$assigned = $_POST['name'];
$deadline = $_POST ['deadline'];
$sql = "INSERT INTO ".$_SESSION['pname']."_activities
(actname, assigned, deadline)
VALUES
('$actname', '$assigned', '$deadline')
";
$query = mysql_query($sql);
echo $_SESSION['pname'];
?>
<body>
</body>
</html>
You should close the window when the response is returned, not when you submit the page. And as #Marc B pointed out, you have major security problem!

textbox number type in html cut off 0 if post form in php

If i give textbox type as number and submit a form it cut off 0 .
ex: 099921 means it will post as 99921
why it cut off preceding 0
Because 0 in front means nothing and is removed. Make it a string (text) and it will stay. Ints have no 0 in front.
i dont know why but i did the same code and it is displaying the value with o i.e 099921
check out my code
<?php
if(isset($_POST['submit']))
{
echo $numberValue = $_POST['numVal'];
}
?>
<!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>Untitled Document</title>
</head>
<body>
<form method="post">
<input type="number" name="numVal" value="" />
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html

How to change dropdown selection into typeahead which using mysql database?

I want to change this code (dropdown select course) into typeahead so i just need to type a few word and then, it will suggest.
<TD width="112"><INPUT type="checkbox" name="chk[]"/></TD>
<TD width="472">
<SELECT name="course[]">
<?php
$sql = "select * from tt_course";
$result = mysql_query($sql);
while($rs = mysql_fetch_assoc($result))
{
$select="";
echo "<option value=\"".$rs['course_id']."\" ".$select.">".$rs['course_name']."</option>";
}
?>
</SELECT>
</TD>
Have a look at http://jqueryui.com/demos/autocomplete/
Its simple and easy to setup and will serve the purpose. You can create an array of values and provide it to Autocomplete or you can make it call your php page and parse the returned JSON data.
As promised, here is an example code to get you going. Please note that this code uses a local javascript variable to prepare suggestions for the autocomplete box. You can easily update that to look for JSON data from your php page and i could easily have made you even that but purpose is to help you learn so i think this way it would be better for you so that you can see how it works and then update it to your needs accordingly. Here you go, you can simply copy paste this code and see it running
<!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 rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/black-tie/jquery-ui.css" type="text/css" />
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js'></script>
<script>
$(document).ready(function() {
$( "#AC" ).autocomplete({
source: programmingLang
});
});
var programmingLang = ["ActionScript","AppleScript","Asp","BASIC","C","C++",
"Clojure","COBOL","ColdFusion","Erlang","Fortran","Groovy","Haskell",
"Java","JavaScript","Lisp","Perl","PHP","Python","Ruby","Scala","Scheme"];
</script>
</head>
<body>
<input type=text name="AC" id="AC">
</body>
</html>

Categories