Using PHP to change HTML elements [closed] - php

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 8 years ago.
Improve this question
I have a simple HTML page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p id="demo">Nothing here</p>
<button onclick="?">Button</button>
</body>
</html>
and a separate PHP file:
<?php
// Load and exec craigslist
$site = curl_init("https://www.craigslist.org/about/sites");
curl_setopt($site, CURLOPT_RETURNTRANSFER, true);
$target = curl_exec($site);
$dom = new DOMDocument();
#$dom -> loadHTML($target);
// Save logo text
$title = $dom -> getElementById('logo') -> nodeValue;
?>
Here's what I want to do:
When I click the button on the HTML page, I want the PHP script to run so that I can get the craigslist title and store it a PHP variable ($title). I want to replace the text in <p id="demo> ("Nothing here") with the text stored in $title ("craigslist").
How do I do this?

Change your html to this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p id="demo">Nothing here</p>
<button onclick="myFunc()">Button</button>
<script>
function myFunc() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("demo").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","file.php",true); //Change file.php to the location of your php file
xmlhttp.send();
}
</script>
</body>
</html>
And add this line to your PHP file after setting the title variable
echo $title;
Now of course best case scenario you would use jQuery's built-in AJAX but this works well enough

Related

Add html inside any tag without javascript

Guys, I have a problem. Is there a way to add html inside a tag without using javascript using only php anyway ?. Thank you very much for your help in advance.
For example, there is this code:
<?php
// This part is required here, because she comes another function.
// It's generate from php server, I need to show inside tag body, for example.
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div"></div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
<script>
$('#my-div').html($('#titulo').html());
</script>
</body>
</html>
The output is this in the source code:
In the browser, the output is this My String:
But, this manipulation is the gift, which uses javascript for this. I don't want it that way. This will not do, because the code will be shown at the top, before the <! DOCTYPE html> tag. Is it possible, on the server, to insert <h1 style ='display:none' id='title'> My String </h1> inside the boby tag, for example?
How I would like it to look:
Example 2
For example, I have this file with code:
file2.php
<?php
include "file2.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
I want to show "<h1>My String</h1>" here.
</div>
</body>
</html>
Yes you can do it as simple as this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
<?php
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
</body>
</html>
PHP is a server side scripting language and will only parse PHP code on the server side.
Yes, you can place HTML code inside PHP variables like you have done, but that will get rendered into the client (your browser).
What you can do is place $code variable inside the target div, like this:
<div id="my-div"><?php echo $code; ?></div>
Give it a try

Mathjax not rendering TEX formulas dynamically from PHP/Ajax

var questions = {"ques_id":"1","question":"<p><span class=\\\"math-tex\\\">\\\\(x = {-b \\\\pm \\\\sqrt{b^2-4ac} \\\\over 2a}\\\\)<\/span> Test Question new with mathjax<\/p>","ques_type":"text_based_questions","correctAnswer":3,"choices":[{"option_id":"1","value":"Option A"},{"option_id":"2","value":"Option B"},{"option_id":"3","value":"Option C"},{"option_id":"4","value":"OPtion D"}]};
$('#test-question').html(questions.question);
MathJax.Hub.Queue(["Typeset",MathJax.Hub, 'test-question']);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
showProcessingMessages: false,
tex2jax: { inlineMath: [['$','$'],['\\(','\\)']] }
});
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_HTMLorMML">
</script>
</head>
<body>
<h5 class="border-bottom">Instructions: Read the question, work out your answer and select the best option.</h5>
<p><span class="math-tex">\( \left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right) \)</span></p>
<div class="question" id="test-question"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
</body>
</html>
I'm getting the following question from Ajax request
MathJax is configured properly but the question coming from the JSON is not rendered by MathJax. The question are saved in MySQL database and are fetched by ajax request.
I tried everything but it still not working. Please help
From Json it added double slash. Plz remove that it will solve your problem.

PHP index.php including structure [closed]

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 9 years ago.
Improve this question
I need help. On my site I use URL parameters to decide which other PHP files to include on the page. My first question is: What should be in index.php and what should be in the included PHP file?
On the Internet I found instructions, suggesting this structure for index.php:
<html>
<head>
<?php include 'header.php'; ?>
</head>
<body>
<?php include 'menu.php'; ?>
<?php include 'content.php'; /* Including page based on the parameters in the url */ ?>
</body>
</html>
With this structure, how can I change the data in the <head> section based on the content in content.php? For example, for index.php?option=article&id_article=1, I will include article.php and show the article with id 1. How, then, can I change the <title>, <meta>, etc content when <head> was written before including the article?
Thanks!
One option that is kind of ugly but will work is instead of having header.php echo have it simply set variables like $title and $meta[]. Also instead of having article.php from echoing return a variable like $html. Also in article.php you can then overwrite any of the variables set in header.php. Then you can construct your index.php like so:
<?php include 'header.php'; ?>
<?php include 'article.php'; ?>
<html>
<head>
<?php echo $title ?>
</ head>
<body>
<?php include 'menu.php'; ?>
<?php echo $html ?>
</ body>
</ html>
Or you can look into ob_start() and ob_flush() etc...
To be as simple as possible, you could make your header a function, then call the function elsewhere...
Example (untested):
function drawHeader($title, $desc = "", $keywords = "", $extra = "") {
?>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $desc; ?>">
<meta name="keywords" content="<?php echo $keywords; ?>">
<link rel="stylesheet" type="text/css" href="path/to/my.css">
<?php echo $extra; ?>
</head>
<?php
}
The goal of the above would be so you could easily and quickly do something like...
<!DOCTYPE html>
<html>
<?php drawHeader("Home", "Some description", "Some, keywords, here"); ?>
<body>
<h1>Hello, world</h1>
<?php drawFooter(); // maybe a footer of some type? ?>
</body>
</html>
Or you could set the variables before calling the included file... and in the included file simply echo those values in the proper places.
There's tons of ways to do this, lots of standards and best practices, frameworks, templating systems, placeholders using output buffering, etc.
first the instructions you found have nothing to learn from
as second to get the content of the file article.php
using the url index.php?option=article&id_article=1
you will need to use the $_GET['id_article']
Example :
$page = $_GET {'id_article'};
if (isset($page)) {
$url = $page.".php";
if (file_exists($url)) {
include $url;
}
and you can use database for storing the articles and use the query then using
if ($_REQUEST['id_article'] == $page) {
$querythearticle = mysql_query("select tablename from database_name where id_article='$page'");
}

Generating XML which contains HTML in PHP - Problems with variables [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am sitting on a little problem here:
I have a php file which generates xml data.
$requestXmlBody .= "<Version>$compatabilityLevel</Version>";
Now there are variables pulled from the upper php code and also HTML is generated
$requestXmlBody .=
'<Description>
<![CDATA[
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
<!--some JS-->
</script>
<img src="http://www.myserver.com/pic.jpg" class="etalage_thumb_image" />
</body>
</html>
]]>
</Description>';
Now strangely I cannot mix variables and HTML Code.
As you can see I use CDATA for the HTML. I want to use a variable for the image name rather than a fixed link. So the code would look like this
$requestXmlBody .= '<Description>
<![CDATA[
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
<!--some JS-->
</script>
<img src="$imagelink" class="etalage_thumb_image" />
</body>
</html>
]]>
</Description>';
But this just does not work. I tried this
$requestXmlBody .= '<Description>
<![CDATA[
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
<!--some JS-->
</script>
<img src="]]>$imagelink<![CDATA[" class="etalage_thumb_image" />
</body>
</html>
]]>
</Description>';
But also this will not work. I even tried to hand over the php variable (which I grab from a session btw) to a JS variable and include it with document.write
Still no success.
This one would work
$requestXmlBody .= '<Description>
$imagelink
</Description>';
But not together with the generated HTML code as you can see above.
Any help is appreciated.
Thanks
Separate concerns. Don't do several things at once. If you split your embedded HTML into its own variable, it gets much easier.
As soon as you have 'freed' the HTML string from the XML context, you'll see that the problem still exists. It is caused by quoting the string with single quotes, which prevent interpolation. You have to use string concatenation instead of embedding the variable directly.
$description = '<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
<!--some JS-->
</script>
<img src="' . $imagelink . '" class="etalage_thumb_image" />
</body>
</html>';
$requestXmlBody .= '<Description>
<![CDATA[' . $description . ']]>
</Description>';
Be sure that your HTML string does not contain a CDATA section itself, since CDATA sections cannot be nested.
The best approach will be to use the writeCData method.
$link= 'link goes here';
$imagelink = '<img src="'.$link.'" /> ';
// serve xml doc as xml
header('Content-type: application/xml');
// set up the document
$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('description');
// CData output
$xml->writeCData($imagelink);
$xml->endElement();
// end the document and output
$xml->endElement();
echo $xml->outputMemory(true);

Saving html file by POST only works once

I have a html page (database.html) that contains a table you can edit via javascript. The following PHP script is used for saving the the page.
The PHP script saves the updated version of database.html and redirects to the new version (same file).
The problem is that it doesn't work the second time you press save, only the first time.
Any ideas what the problem can be?
HTML:
<!DOCTYPE html><head>
<title>Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
#textarea_database {
display:none;
}
</style>
<script src="js.js"></script>
</head>
<body>
<a id="a_save" href="#">Save</a>
<form id="form_database" method="post" action="save.php">
<textarea id="textarea_database" name="textarea_database"></textarea>
</form>
<div id="contenteditable" contenteditable="true">
Write something and press save...
</div>
</body>
JavaScript:
window.onload = function () {
var a_save = document.getElementById('a_save'),
form_database = document.getElementById('form_database'),
textarea_database = document.getElementById('textarea_database');
a_save.onclick = function () {
textarea_database.value = document.documentElement.innerHTML;
form_database.submit();
}
}
PHP:
<?php
// the textarea contain all th html
$textarea_database = $_POST["textarea_database"];
// add doctype since javascript document.documentElement.innerHTML dont get it
$textarea_database = '<!DOCTYPE html>' . $textarea_database;
// update the html database file
$open_database = fopen('database.html','w+');
fputs($open_database,$textarea_database);
fclose($open_database);
// redirect to the uppdated database (timestamp prevent browser cache)
$the_time = date('Y-m-d-H-i-s');
$url_and_time = "database.html?" . $the_time;
header("Location: $url_and_time ");
exit;
?>

Categories