Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
two php variable in php code..
$areaid = filter_var($_GET["aid"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
$areaname = filter_var($_GET["aname"], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$(document).ready(function() {
$("#submit_btn").click(function() {
var uareaname = <?php echo ($areaname) ?>;
var uareaid = <?php echo ($areaid) ?>;
post_data = {'userArea':userareaname,'userAreaid':uareaid}; Not able to post this data
$.post('review_me.php', post_data, function(response){
Okay replace these lines:
var uareaname = <?php echo ($areaname) ?>;
var uareaid = <?php echo ($areaid) ?>;
With these line:
var uareaname = '<?php echo ($areaname) ?>';
var uareaid = '<?php echo ($areaid) ?>';
String values must be in quotes. Otherwise it's considered as variables.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I want to show the server status of an IP address on every page, but to check the status I need a PHP script. This script is what I found on the Internet:
<?php
$server = 'google.be:80';
$split = explode(':', $server);
$ip = $split[0];
$port = (empty($split[1])) ? '80' : $split[1];
$server = $ip . ':' . $port;
$fp = #fsockopen($ip, $port, $errno, $errstr, 1);
if($fp) {
echo $server . ' is online';
fclose($fp);
}
else {
echo $server . ' is offline';
}
?>
I want the echoes to be formatted like my CSS content is formatted, so I could just replace the echoes with:
?>
<p>Server is offline<p>
<?php
and
?>
<p>Server is online<p>
<?php
But then I would have to make every HTML file a PHP file. Would you recommend that or is there a different way to handle this?
On my server all the files are a PHP since I need to include PHP functions such as echo username and such, and I believe it doesn't hurt to convert .html to .php. Another thing is that the following page provides information on styling PHP echoes with CSS.
How can I style a PHP echo text?
I think it would be better have all PHP files.
You could use jQuery AJAX to send the PHP data to your HTML page. You could json_encode the response and receive that data as a JSON object and get the data out of it.
EDIT: In a production enviroment and for efficiency, it would be best if you convert the HTML files to PHP files, it will be worth the labour. However this little snippet below could be used for other functionality if modified or built upon so it's a learning experience for you to see basic jQuery AJAX calls.
The following code is a working example of calling your PHP file and getting back the result. Seeing a basic example of using jQuery and AJAX will help you get a firm grounding of how to use it.
check_server.php
<?php
$server='google.be:80';
$split=explode(':',$server);
$ip=$split[0];
$port=(empty($split[1]))?'80':$split[1];
$server=$ip.':'.$port;
$fp = fsockopen($ip, $port, $errno, $errstr, 1);
$result = new stdClass();
if($fp){
$result->result = 'success';
fclose($fp);
}
else{
$result->result = 'offline';
}
echo json_encode($result);
?>
index.html
<html>
<head>
<title>Website</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$.ajax({
url : "check_server.php",
type : "POST",
dataType: "json",
success : function(results){
if (results.result === 'success')
{
$('#status').append('Server online.');
}
else
{
$('#status').append('Server offline.');
}
},
error : function()
{
$('#status').append('An error has occurred.');
}
});
</script>
</head>
<body>
<div id="status"></div>
</body>
</html>
It is not possible to implement PHP in an HTML file. To create HTML in a .php file is the best solution to solve this.
You can use HTML in a PHP file and you do not have to use PHP in the file if you name it a PHP file.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
As mentioned in the title, I am attempting to combine PHP and HTML so that my passwords are not visible.
My code is:
<?php
$host = "localhost";
$user = "**************";
$pass = "**************";
$databaseName = "**************";
$tableName = "**************";
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);
$result = mysql_query("SELECT * FROM $tableName");
$array = mysql_fetch_assoc($result);
$json_string = json_decode(json_encode($array), true);
$ID = $json_string['ID'];
$ENOM_UserID = $json_string["ENOM_UserID"];
$ENOM_Password = $json_string["ENOM_Password"];
?>
<html>
<head>
<script language="javascript" type="text/javascript" src="/content/scripts/jquery/v2.1.3/jquery-2.1.3.js"></script>
</head>
<body>
<h2>Client example </h2>
<h3>Output: </h3>
<div id="output">this element will be accessed by jquery and this text replaced</div>
<script id="source" language="javascript" type="text/javascript">
$('#output').html("<b>ID: </b>" + $ID + "<b> UserName: </b>" + $ENOM_UserID + "<b> Password: </b>" + $ENOM_Password);
</script>
</body>
</html>
How do I reference those variables defined in the PHP section above?
In the HTML contents, wrap the PHP variables into PHP tags, otherwise they will be treated as regular HTML content.
<?php echo $ID?>
or
<?=$ID?>
So the jQuery call might look like this:
$('#output').html("<b>ID: </b><?=$ID?><b> UserName: </b><?=$ENOM_UserID?><b> Password: </b><?=$ENOM_Password?>");
If you just want to hide your password with another character, simply use
<?php preg_replace('/./','*',$ENOM_Password)?>
So the jquery part should be like this:
$('#output').html("<b>ID: </b><?=$ID?><b> UserName: </b><?=$ENOM_UserID?><b> Password: </b><?php preg_replace('/./','*',$ENOM_Password)?>");
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
how do you create an if else statement that contains an include statement?
In ASP you need to have the double quotes but I am not sure how to do it in PHP.
I believe the issue lies with this:
<?php include 'i_main-nav-wohl.php' ?>
I Tried the following:
<?php $url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/occupational/wohl/") === 0) {
echo '<?php include ''i_main-nav-wohl.php'' ?>';
} else {
echo '<?php include ''i_main-nav-wohl.php'' ?>';
}
?>
<?php $url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/occupational/wohl/") === 0) {
echo '<?php include "'i_main-nav-wohl.php'" ?>';
} else {
echo '<?php include "'i_main-nav-wohl.php'" ?>';
}
?>
<?php $url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/occupational/wohl/") === 0) {
echo '<?php include 'i_main-nav-wohl.php' ?>';
} else {
echo '<?php include 'i_main-nav-wohl.php' ?>';
}
?>
Echoing in php means that it displays in the browser and does not implement itself. Just set the include directly in the if statement instead of echoing it.
You dont need a second php tag within your if-else-statement. Otherhwise your PHP output will result in a PHP document containing the content of your if or else branch.
Thus, if you want to produce conditional HTML or CSS use echo. Otherwhise just write your PHP commands without additional php tags.
You can do that like that :
<?php
$url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/occupational/wohl/") === 0) {
include 'i_main-nav-wohl.php';
} else {
include 'i_main-nav-wohl.php';
}
?>
Or even better :
<?php
$url = $_SERVER["REQUEST_URI"];
$fileToInclude = strpos($url, "/occupational/wohl/") === 0 ? 'i_main-nav-wohl.php' : 'i_main-nav-wohl.php';
include($fileToInclude);
?>
BTW you are including the same file in both cases.
You shouldn't echo php tag in php code .
If you want your php code to be executed by your server,
Instead of this code :
echo '<?php include ''i_main-nav-wohl.php'' ?>';
just do this :
include 'i_main-nav-wohl.php';
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a PHP variable which I am trying to pass into a javascript function. Using pure PHP I am to echo the desired string output, but when I shove it into a variable and output it in javascript it doesn't work.
It seems javascript doesn't seem to see anything at all.
Here's the code:
<?php $a = get_post_thumbnail_id( $post -> ID ); ?>
<?php $img = wp_get_attachment_image_src( $a ); ?>
<?php $b = $img[0]; ?>
<script type="text/javascript">
var myVar = <?php echo $b; ?>;
alert(myVar);
</script>
Whilst this is just a test piece of code I'm trying to make work, the results of which I am trying to make work with something like this:
<?php $a = get_post_thumbnail_id( $post -> ID ); ?>
<?php $img = wp_get_attachment_image_src( $a ); ?>
<?php $b = $img[0]; ?>
<script type="text/javascript">
$(".imgWindow").backstretch("<?php echo $b; ?>");
</script>
There's clearly some underlining principal of PHP and Javascript I must be missing.
Enlighten me please. Help appreciated.
<?php $a = get_post_thumbnail_id( $post -> ID ); ?>
<?php $img = wp_get_attachment_image_src( $a ); ?>
<?php $b = $img[0]; ?>
<script type="text/javascript">
var myVar = '<?php echo $b; ?>';
alert(myVar);
</script>
You have to wrap the value in quotes for non-integer values.
Alternatively, assign like this:
var myVar = '<?=$b;?>';
if it's a string "image name" use quotes
var myVar = '<?php echo $b; ?>';
I have following script printed from PHP . If some one has a single quote in description it shows javascript error missing ; as it thinks string terminated .
print "<script type=\"text/javascript\">\n
var Obj = new Array();\n
Obj.title = '{$_REQUEST['title']}';
Obj.description = '{$_REQUEST['description']}';
</script>";
Form does a post to this page and title and description comes from textbox.Also I am unable to put double quotes around {$_REQUEST['title']} as it shows syntax error . How can I handle this ?
a more clean (and secure) way to do it (imo):
<?php
//code here
$title = addslashes(strip_tags($_REQUEST['title']));
$description = addslashes(strip_tags($_REQUEST['description']));
?>
<script type="text/javascript">
var Obj = new Array();
Obj.title = '<?php echo $title?>';
Obj.description = '<?php echo $description?>';
</script>
You also need to be careful with things like line breaks. JavaScript strings can't span over multiple lines. json_encode is the way to go. (Adding this as new answer because of code example.)
<?php
$_REQUEST = array(
'title' => 'That\'s cool',
'description' => 'That\'s "hot"
& not cool</script>'
);
?>
<script type="text/javascript">
var Obj = new Array();
Obj.title = <?php echo json_encode($_REQUEST['title'], JSON_HEX_TAG); ?>;
Obj.description = <?php echo json_encode($_REQUEST['description'], JSON_HEX_TAG); ?>;
alert(Obj.title + "\n" + Obj.description);
</script>
Edit (2016-Nov-15): Adds JSON_HEX_TAG parameter to json_encode calls. I hope this solves all issues when writing data into JavaScript within <script> elements. There are some rather annoying corner cases.
Use the string concatenation operator:
http://php.net/manual/en/language.operators.string.php
print "<script type=\"text/javascript\">\n
var Obj = new Array();\n
Obj.title = '".$_REQUEST['title']."';
Obj.description = '".$_REQUEST['description']."';
</script>";