echo text alongside a generated value PHP - php

I'm using this line of php in my main page
echo generateRadioButtons("fbresponse.php", "moRating1", 6);
Which when posting the following on the response file
echo $_POST['moRating1']
It works fine and displays the correct result, but! my question is how would i add text to that so..
Blah blah blah, you rated x question: 'moRating1'
I've tried doing
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $moRating1 ?></p>
</body>
</html>
inside the response file but that just doesnt load anything..
Any help please!

It's probably because this function uses eval() to execute its content (I guess it from lack of PHP tags in your first example).
If it's true, then you should be able to close PHP tag, print HTML and open it again.
?>
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $_POST['moRating1'] ?></p>
</body>
</html>

try doing:
$mRating1 = $_POST['moRating1'];
...
?>
...
<p>How well did you rate it: <?php echo $mRating1?></p>

Related

I'm using trumbowyg as text editor for the textareas in my forum, but how do I echo it?

I didn't find any post related to my problem, so here I go :
I added trumbowyg (it's a WYSIWYG editor) to edit the content in my <textarea></textarea>, and it works just fine when I post it in my database.
Only problem is : how do I echo it ?
The parsing method of trumbowyg takes this form : you click on, let's say B in the toolbar on top of the textarea, and it will put your text in bold weight. But in the server, once posted, it takes actually this form : <strong>some text</strong>.
Obviously, when I echo the var stocking the data in this part of my sql request, it output it the same way : <strong>some text</strong> and not some text.
I don't know if it's actually so simple that I can't seem to find the solution, or if I'm trying something impossible... ?
Thanks by advance guys !
Well... Guess it was so obvious that I didn't find the answer in here. If it can help people who find themselves in the same situation as me : just wrap your var containing html with html_entity_decode($var)
That's it.
See below (textarea is showed if the user consulting the profile is the one wich it belongs to, else it just echo the descrption (called in an Action.php file, I didn't put the "requires" before the <!DOCTYPE html> declaration.
<!DOCTYPE html>
<html lang="en">
<?php include "includes/head.php"; ?>
<body>
<?php include ("includes/navbar.php") ?>
<div class="container">
<h2><?= $user_pseudo;?></h2>
<h5><?= $user_access_level;?></h5>
<?php
if($_SESSION['id'] == $user_id){
?>
<form method="POST">
<textarea id="parse" name="description"><?= $user_description; ?></textarea>
<button class="btn btn-primary" name="validate" type="submit">Mettre à jour</button>
</form>
<?php
} else {
?>
<br/><br/><br/>
<div class="container">
<?= html_entity_decode($user_description) ?>
</div>
</div>
<?php
}
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-3.3.1.min.js"><\/script>')</script>
<script src="trumbowyg/dist/trumbowyg.min.js"></script>
<script>
$('#parse').trumbowyg();
</script>
</body>
</html>

PHP - replacing dot into arrows

I have this data in my database that i fetched:
As you can see i store an html template in my database.
This is the code i used to output that html:
$php = Blade::compileString($template->content);
dd($php) //i used laravel framework, btw.
Then this is the output:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exmaple.com</title>
<style>
</style>
</head>
<body>
<div class="account-details">
<b>Account Name</b>: <?php echo e(company.name); ?><br>
<b>Plan</b>: <?php echo e(plan.name); ?><br>
<b>Source</b>: <?php echo e(source.name); ?><br>
<b>Source Type</b>: <?php echo e(source.source_type.name); ?><br>
<br>
</div>
<div class="welcome" style="font-family:Arial;font-size:small">
Hi <?php echo e(user.first_name); ?>,<br>
<br>
There seems to be a problem blah blah blah<br>
<br>
Details of the problem are:<br>
<?php echo e(sourceMessage); ?><br>
<br>
You can check and update the source here:<br>
https://example.com<br>
<br>
<br>
<span style="font-size:12.8px">
Kind regards,<br>
<br>
Test Team<br>
<br>
Email: example.com<br>
Website: example.com<br>
</span>
</div>
</body>
</html>
I want to change the .(dot) into -> so in this example, some of text will output like:
from <?php echo e(company.name); ?> to <?php echo e(company->name); ?>
from <?php echo e(source.name); ?> to <?php echo e(source->name); ?>
So i think if it's in a <?php echo e(whatever); ?> that's the time we check and replace the . with -> ?
I think this can be done by RegEx but I'm not expert on that.
The reason why i wanted to replace it is because i am getting a template from an email service then returns ., so i wanted to replace that with -> because I know PHP reads -> in accessing objects rather than ..
You can use preg_replace to look for pieces of code matching <?php ... e(f) and replace the .s in f with ->:
$html = preg_replace_callback('/(<\?php\s+.*?\be\()([^)]+\))/',
function ($m) {
return "{$m[1]}$" . str_replace('.', '->', $m[2]);
},
$html);
Note we use a callback as it makes it easier to deal with replacing a.b.c with a->b->c. Also, to really look like PHP, you need to add a $ at the beginning of the variable name, which this code does. If you don't want it, just change {$m[1]}$ to {$m[1]}
Demo on 3v4l.org
If you're doing that on a editor like VS code or sublime, you just need to replace the function e usage parts.
CTRL + H
(In sublime or VS code) to open up replace dialog.
Make sure the Regex option is clicked.
This would do the trick;
Search (e\(.+)\.(.+\))
Replace $1->$3

How to set title with data from included PHP file

I have a website with a template page that is something like this
<html>
<head>
<title>{{TITLE}}</title>
</head>
<body>
<div id = "header"> ... </div>
<?php include 'content.php'; ?>
<div id = "footer"> ... </div>
</body>
</html>
And then content.php would look something like this
<?php
$title = "xx";
#other php code here
?>
<p> more content </p>
My question is whether there is some way to set this up so that I am able to set the title from the file included in the middle of the page (without using javascript). I know that most people suggest including it at the top but if I were to do that the html would be at the top instead of between the header and the footer. I've wracked my brains for a while and I haven't really figured out a good way to do this (and there are a variety of possible files to be included; content.php is just an example, so I really do need some way to do this dynamically). I want to avoid putting too much code outside of the template. Any ideas?
Use output buffering:
<?php
ob_start();
include 'content.php';
$content = ob_get_clean();
?>
<html>
<head>
<title><?=htmlspecialchars($title)?></title>
</head>
<body>
<div id = "header"> ... </div>
<?=$content?>
<div id = "footer"> ... </div>
</body>
</html>
I'm not sure if you're using a templating engine or not (something like Laravel's Blade templates allow for this, I believe), but assuming you are using straight PHP, I have found the most efficient way to do this is to approach from the opposite direction and include the template file in each of my content files.
For example, I may have template.php, which has multiple functions, like this:
<?php
function createHeader($title, $keywords) {
//echo or <<<EOD your header with the set variables
}
function createFooter(...) { ... } //etc
And then, in my 'child' files, I would do:
<?php include('template.php'); ?>
<?php createHeader("My Website Front Page!", "fun, good times, joy"); ?>
<h1>My page content</h1>
<p>Content goes here</p>
<?php createFooter(...); ?>
This is a different structure from what you were attempting, though, and may not retain your intended structure.

Hide Div From those who visit directly to the page but don't who refer from a exact domain using php

I want to hide a div from direct users but showing the same div to those who come from example.com
eg.
example123.com/article.php have below div
<div id="main">Title</div>
(when user click on a hyperlink on example.com
Artile
then show the above div
but when user come directly to example123.com/article.php then don't show the div.
how will I do that using php?
Hi you can use the following code like this.
<?php if (isset($_SERVER['HTTP_REFERER'])){ ?>
<div style="width:200px;height:200px;border:1px solid black">
<?php } ?>
You need to make use of $_SERVER['HTTP_REFERER']
I don't totally follow your question but this code should be enough for you to adapt to your needs:
if(strstr($_SERVER['HTTP_REFERER'],'example.com')) {
echo '<div id="main">Title</div>';
}
So, if the referrer URL contains example.com then echo your div.
If the referral URL didn't contain example.com or was empty (i.e. they arrived directly at your site) then the div won't show.
You can achieve this by passing an argument from the URL. The value of the argument will be null if they access the page directly and only have a value if they use the specific URL. Then your PHP can just check the argument and handle it accordingly.
Example as follows.
index.php
<!DOCTYPE html>
<html>
<head>
<title>Nothing</title>
</head>
<body>
<h1>Nothing 01</h1>
Regular URL
<br />
Argument URL
</body>
</html>
Then you can handle the Arguments in your PHP page containing the div
pagewithdiv.php
<!DOCTYPE html>
<html>
<head>
<title>Nothing</title>
</head>
<body>
<h1>Nothing 02</h1>
<div id="conditional">
<h2>Conditional Div</h2>
</div>
<?php
if (
// check if argument exists
isset($_GET["Condition"])
&&
// check if argument value is true
trim($_GET["Condition"] == true)
) {
echo '<script>';
echo 'document.getElementById("conditional").style.display = "block"';
echo '</script>';
} else {
echo '<script>';
echo 'document.getElementById("conditional").style.display = "none"';
echo '</script>';
}
?>
</body>
</html>
Keep in mind though that this is only hiding the div, it still exists on the page. If you want it to be completely gone then instead of using javascript to change the visibility you can echo the code that makes up the div if the requirements are met.
<!DOCTYPE html>
<html>
<head>
<title>Nothing</title>
</head>
<body>
<h1>Nothing 02</h1>
<?php
if (
// check if argument exists
isset($_GET["Condition"])
&&
// check if argument value is true
trim($_GET["Condition"] == true)
) {
echo '<div id="conditional">';
echo ' <h2>Conditional Div</h2>';
echo '</div>';
}
?>
</body>
</html>

Include php file in html page

I'm working with XML and PHP to populate a web form drop down box.
I have a html page
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<header>
<h1>Title</h1>
</header>
<form>
<div id ="select xml">
<?php include 'dropdown.php'; ?>
</div>
</form>
</body>
</html>
and I am hoping to include the following PHP to generate the actual box with the file names of the XML.
<?php
//echo(substr(glob("xml/*.xml")[0],4));
echo "<p>
<label>Select list</label><br>
<select id = \"selectxml\">
<option value'0'>--Please Select--</option>";
$count = count(glob("xml/*.xml"));
$files = glob("xml/*.xml");
for ($i = 0; $i < $count; $i++) {
//echo(substr($files[$i],4));
//echo "<br>";
$filename = (substr($files[$i],4));
echo "<option value=$i+1>$filename</option>";
}
echo "</select><br>
<br>
</p>";
?>
I'm aware the PHP isnt perfect, but it works.
The issue is that the Include HTML does not work when I run the page - any ideas?
Change the extension to .php and you will be okay .
When the page have .html extension, the web server doesn't recognize it as a PHP file, and you can't use PHP codes in it. and any PHP code, will be processed as a plain text .
I mean when you put :
<?php echo "hello" ?>
in a HMTL page, browser will show :
<?php echo "hello" ?>
But, if the page have .php extension, browser will show :
hello
Rename your html page from .html to .php
it;s impossible, but with jQuery.load function you can do this :
$("#select-xml").load("dropdown.php");

Categories