I have a php file with the following code which is run inside a switch statement:
switch($valueFoo) {
case 'bar':
echo "<select id=\"selTheme\">";
$path = './files/css/themes/';
$files = array_values(array_diff(scandir($path), array('.', '..')));
for ($i = 0; $i < count($files); $i++) {
$cVal = substr($files[$i], 0, -4);
$cTitle = ucwords(substr($files[$i], 0, -4));
if ($cTitle==$_SESSION['setTheme']) {
echo "<option value='" . $cVal . "' onclick=\"changeColors('" . $cVal . "')\" selected>" . $cTitle . "</option>";
} elseif ($i>=count($files)) {
echo "<option value='" . $cVal . "' onclick=\"changeColors('" . $cVal . "')\">" . $cTitle . "</option></select>";
} else {
echo "<option value='" . $cVal . "' onclick=\"changeColors('" . $cVal . "')\">" . $cTitle . "</option>";
}
}
echo "...";
This is intended to create a list of options from a folder on my server. And it does indeed work. The problem is that it ONLY works AFTER the page is refreshed. I have been banging my head on my table trying to figure out why it does this only after the page is refreshed. I have no clue. I want the element to be filled with options as soon as it loads on page. I don't want the page to reload at all. It works by itself to populate an unordered list but I want it to be selectable options.
I don't see anything wrong with the code at all. I don't understand why the options list aren't being populated without a reloading of the page. I don't understand why it fills in perfectly when the page reloads. I would think that if it would do it properly AFTER the reload, it would do it just fine the first time it loads! Why isn't?
Please help me understand.
EDIT: This code comes as a return from an AJAX call. I am trying to run the for loop from that AJAX call. The loop doesn't run until the page reloads. Is there a way to force the AJAX call without the page load?
if you want a dynamic display of the folders content you'll have to use Ajax.
make sure that the session is already created and you're using 'session_start()' in the beginning of all your pages,
clarify your question/code to get precise answers
Related
I'm having trouble sending a parameter into a url of another page. I'm new to coding in PHP so I do not really know how to get this through, already did some research on this about the $_GET method, but its still not working.
Code in 1st page:
echo "<tr><td><a href='application_desktop.php?id='". $temp ."'>" . $row['appl_nric_date'] . "</td><td>" . $row['applicant_name'] . "</td><td>" . $row['nric'] . "</td><td>" . $row['application_date'] . "</a></td></tr>";
where $temp is the parameter I want to pass to the url.
Code in 2nd page:
$id = $_GET['id'];
$applicants = mysql_query("SELECT * FROM tblapplication WHERE appl_nric_date = $id");
//$applicants = mysql_query("SELECT * FROM tblapplication WHERE appl_nric_date = 10");
The sql query returns error that the $id is null, and the url doesn't display the id.
Do it like this on your html line
echo "<tr><td>" . $row['appl_nric_date'] . "</td><td>" . $row['applicant_name'] . "</td><td>" . $row['nric'] . "</td><td>" . $row['application_date'] . "</td></tr>";
I ran your code to see what it output:
// Make some sample data
$row = [
'appl_nric_date' => '9999-99-99',
'applicant_name' => 'some-applicant',
'nric' => 'wtf-is-an-nric',
'application_date' => '8888-99-00'
];
$temp = 'something';
echo "<tr><td><a href='application_desktop.php?id='". $temp ."'>"
. $row['appl_nric_date']
. "</td><td>"
. $row['applicant_name']
. "</td><td>"
. $row['nric']
. "</td><td>"
. $row['application_date'] . "</a></td></tr>";
echo PHP_EOL;
This is what it outputs:
<tr><td><a href='application_desktop.php?id='something'>9999-99-99</td><td>some-applicant</td><td>wtf-is-an-nric</td><td>8888-99-00</a></td></tr>
The use of single quotes is not right. Remove the single quote after id=.
Looks like you are not nesting your html elements correctly. You place the opening A tag inside the first TD but then you close that TD without closing the A tag.
In order to debug what you are doing this in the browser, then the address bar where the url lives should show the parameters that are sent to the destination page. You can just look at that to verify that it sent what you intended.
In the destination page, you can add the following to debug:
<pre>
<?php print_r($_GET) ?>
</pre>
The above will let you see what you are getting from the first page.
I'm working on a simple script - file input which will change the site's background to the given image. It works, but my problem starts when I refresh site - the background image disappears.
I was wondering how to set and check if the background was set, so it will be there as long since next file input ?
I was trying to do that with a constant but does not work, here is my code:
if (isset($_POST['submit_bgImg'])) {
$myTarget = 'img/' . basename($_FILES['bg_img']['name']);
if (move_uploaded_file($_FILES['bg_img']['tmp_name'], $myTarget)) {
print('<style> body {background-image:url(img/' . $myFile . ');}</style>');
define('MY_BG', $_FILES['bg_img']['name']);
}
}
if (defined('MY_BG')) {
print('<style> body {background-image:url(img/' . MY_BG . ');}</style>');
}
any help ?
If you want to keep it only for the user you must store MY_BG variable in Session or Cookies like :$_SESSION['my_bg'] = $_FILES['bg_img']['name'];
if you want to keep it forever you must store it on a file or a Database like MySQL
$conn = new MySQLi('host','user','password','database name');
$conn->query("INSERT INTO table VALUES ('" . $bg_name . "')");
Try out with echo in php instead of print.I recommended to you can use ajax.
if (isset($_POST['submit_bgImg'])) {
$myTarget = 'img/' . basename($_FILES['bg_img']['name']);
if (move_uploaded_file($_FILES['bg_img']['tmp_name'], $myTarget)) {
echo "<style> body {background-image:url(img/' . $myFile . ');}</style>";
define('MY_BG', $_FILES['bg_img']['name']);
}
}
if (defined('MY_BG')) {
echo "<style> body {background-image:url(img/' . MY_BG . ');}</style>";
}
This is my javascript function.
<script>
function output($file_name, $content)
{
document.getElementById("content_title").innerHTML=" "+$file_name;
document.getElementById("content").innerHTML=" "+$content;
}
</script>
This is my PHP code;
<?php
$dir = "img_png";
$files = scandir($dir);
$dir_length = count($files);
?>
This is the second part of my PHP code (with problem); Problem: When $content="any string"; everything works properly, but when $content=file_get_contents('file'); my triggered function doesn't change any .innerHTML elements at all.
<?php
for ($i=2;$i<$dir_length;$i++){
$title=explode(".png", $files[$i]);
$content=file_get_contents('./content_txt/tv.txt');
echo "<td><button id='button' class='button' onClick=\"output('", $title[0],"";
echo "', '", $content,"";
echo "')\"><img src=\"/img_png/", $files[$i], "\"></img></button></td>";
}
?>
Start solving your issue with this (use dots instead of commas to concatenate strings)
<?php
for ($i=2;$i<$dir_length;$i++){
$title=explode(".png", $files[$i]);
$content=file_get_contents('./content_txt/tv.txt');
echo "<td><button id='button' class='button' onClick=\"output('" . $title[0];
echo "', '" . $content;
echo "')\"><img src=\"/img_png/" . $files[$i] . "\"></img></button></td>";
}
?>
Also I would set $content-variable outside the loop and skip the end-tag (it doesn't make any difference because </img> is not used):
Also encode your data when sending from php (with urlencode)
<?php
$content=file_get_contents('./content_txt/tv.txt');
for ($i=2;$i<$dir_length;$i++){
$title=explode(".png", $files[$i]);
echo "<td><button id='button' class='button' onClick=\"output('" . $title[0];
echo "', '" . urlencode($content);
echo "')\"><img src=\"/img_png/" . $files[$i] . "\"></button></td>";
}
?>
In your javascript you have to decode the content:
function output($file_name, $content)
{
$content = decodeURI($content);
document.getElementById("content_title").innerHTML=" "+$file_name;
document.getElementById("content").innerHTML=" "+$content;
}
Your file contents may contain line breaks or special characters that need escaping in JS, you will need to escape special characters before passing the string to the javascript function.
"tvvvvvv";
I suppose you copy-paste that string from PHP to tv.txt
Remove " and ; - you don't need it in file.
And " is your problem in javascript.
This is a followup to the solution for this question.
I am using jQuery's load() function to pull a headline within a div tag from one page to another within my site. This works wonderfully.
The problem is, load() also pulls the div tag itself, which I do not want, as it then gets formatted via CSS like the source page.
Here is the PHP:
function get_team_articles($team_id, $feat=0) {
.
.
.
while ($row = mysql_fetch_assoc($r)) {
$page = explode('_', $row['page_id']);
(is_numeric($page[1]))
? $pre = 'wk_'
: $pre = '';
$arr[] = $page[0] . " | " . $pre . $page[1] . ": " . "
<a linked_div='news_header'
linked_path='../news/" . $page[0] . "/" . $pre . $page[1] . "/" . $page[1] . "_" . $page[2] . ".html'
href='index.php?view=news&yr=" . $page[0] . "&wk=" . $page[1] . "&pg=" . $page[2] . "'></a>";
}
$articles = implode('<br/>', $arr);
return $articles;
}
Notice the linked_div and linked_path attributes within the anchor tag, which are used in my jQuery:
function set_team_headlines(){
$('#section-articles > a').each(function() {
var a = $(this);
a.load(a.attr('linked_path') + ' #' + a.attr('linked_div'));
});
}
Obviously I cannot strip the HTML tags within the anchor tags in PHP, because the server doesn't have the text within the anchor tags upon loading; so I assume I need to strip the HTML in jQuery after the load() call...and that is what I cannot figure out how to do. :)
The result I want is:
My headline
The result I'm getting is:
<div id="news_header">My headline</div>
Hopefully this makes sense. I think I provided more detail than I needed to. Thanks!
Navigate down one more level in your .load selector
a.load(a.attr('linked_path') + ' #news_header');
If your news_header id isn't unique, it isn't valid to select by that id (ID's must be unique!)
To get around that issue, use this:
a.load(a.attr('linked_path') + ' #' + a.attr('linked_div') + ' div');
Edit:
.load actually includes the targeted element when appending html instead of appending the target element's children. I would move to using $.get().
$.get(a.attr('linked_path')).done(function(html) {
a.text($(html).filter("#news_header").text());
});
I have a huge list of stuff for a glossary ( about 17 pages worth ) that I have to put into an XML file. So I decided I'd use php to make it. My code works, except where ALL the XML code is, it doesn't show because it's trying to render it. Help?
$arg=explode("\n", $strang);
echo count($arg);
for ($i=0;$i<=count($arg);$i=$i+3)
{
echo "<word id='" . $arg[$i+1] . "'>";
echo "<desc>" . $arg[$i] . " - " . $arg[$i+2] . "</desc>";
echo "<pic></pic>";
echo "<audio></audio>";
}
I assume by render it you mean in your browser? If so, you'll need to escape the characters so they will be interpreted literally rather than as markup.
Check out htmlspecialchars and htmlentities
use CDATA construction:
echo "<desc><![CDATA[" . $arg[$i] . " - " . $arg[$i+2] . "]]></desc>";
If this is your entire script, fastest way would probably be to swap all of the <'s with <
$arg=explode("\n", $strang);
echo count($arg);
for ($i=0;$i<=count($arg);$i=$i+3)
{
echo "<word id='" . $arg[$i+1] . "'>";
echo "<desc>" . $arg[$i] . " - " . $arg[$i+2] . "</desc>";
echo "<pic></pic>";
echo "<audio></audio>";
}