Dynamically loading an image from URL into a div - php

So,
basically I have this image gallery > http://sensemillia.com/#/pages/necklaces.php
once the user clicks on one of them images he's lead to the item.php > http://sensemillia.com/#/pages/item.php
As you can see on the left side of the box, there is an image and on the right, there's text.
What I'd like to do, is tell to dynamically item.php load the content depending the url.
Moreover, if the user for example clicks on the first image in the gallery, the url should look like this > ...item.php?id=01 and then accordingly the content of the box should load image01.jpg on the left column, and div01 on the right.
I've searched a lot for this, but all I find are complicated asp or .net answers.
Is there a simple way?
Thank you very much in advance. Any help would be greatly appreciated.
ANSWER
<?php $actual_link = "$_SERVER[REQUEST_URI]";
if ($actual_link == "/pages/item.php?id=11") {
print ("<img src='../Sensemillia/sousou.jpg' id='jack' width='392'
height='475' alt='Daisy on the Ohoopee'/>");
}
else if ($actual_link == '/pages/item.php?id=12') {
print ("<img src='../Sensemillia/second.jpg' id='jack' width='392'
height='475' alt='Daisy on the Ohoopee'/>");
}
?>

You can use session variables or use POST to send data between the pages and fetch the data from your database
How do I pass data between pages in PHP?
PHP Pass variable to next page
Edit:
Maybe you'll want to start here:
http://www.homeandlearn.co.uk/php/php.html
It will give you basic understanding of php and allow you implement that feature on your site.
Also when you're comfortable with mysql queries you'll want to replace them with PDO

Okay let me repeat: You have an image. When you click on the image, a javascript will be called, which loads the page, behind the hash, with ajax?
Why not simply add the ?id=123 to the link behind the hash and let PHP return the values from database, depending on given id?
Btw. I strongly recommend, not to execute the load of page 1:1, because this may cause security issues. This looks a bit like XSS.

Related

Import PHP Variables in a pop up window

I have an original php page and a popup window written in php, I wonder how to import the variables in the original page into the popup window. One possible problem is the variable in the original page is not defined in itself but in other php pages connected to it, and it will change based on user's choice. Sincerely looking forward to suggestions, thanks!
It is like:
Two files "business.php" and "school.php" share a header file called "info.php", in "business.php" $theme = "business"; and in "school.php" $theme = "school".
In header file, I can use
if($theme=="business"){
echo "<a href='#' onclick='popitup();'>Popup</a>";
}
to have a button in the business header part.
The function for the popup window is like:
function popitup() {
var newWindow = window.open('/../popitup.php', 'name');
if (window.focus) {
newwindow.focus()
}
return false;
}
What I hope to have is using the $theme variable in "popitup.php", such as when user click the "Popup" link from business page, the popup page can show a list of options related to business information.
I would suggest something like the following:
first, start a session and maintain that for all of your pages.
The page that calls the main page sounds like the best place to centralize these things. Let's call that your controller although it may be a misnomer depending on your setup. However, if it is where the variables you are talking about are defined, it is likely close enough.
Have it call your popup, being practically persistent now. Problem solved.
You may want to look into Model-View-Controller (MVC) as well as Object Oriented Programming (OOP) concepts. Make no mistake, this is not that, but a good architecture with planning can make things much easier.
TLDR: sessions
here is a link
here is a user friendly link
edit: just incase you do not want to refactor something you have already in place, you can call your popup with a query string at the end of your url and access them with $_GET[] on the php side. Here is a link for that. Then it would be something like, page.php?option1=abcd&option2=efgh I think of that as messy and a bit antiquated, but it is valid and works fine.
another edit: another option is using cookies that you store in the user's client. It is not as fool-proof though because people may not have them on, but it may accomplish what you are looking for. Here is a link for those.
third edit: you can store your user's choices in a database and pull them with code in the popup as it is rendered.

Auto-update Posts without refreshing

I'm currently only using PHP to take user submissions, put them in a database, and echo them out on a page using SQL to select from a table, such as comments. I need a system that will automatically update comments without refreshing the page like on YouTube. The less the user has to manually update, the better.
I want it to work pretty much exactly how YouTube and Twitter function, where it'll say "x NEW COMMENT(s)" and clicking that updates everything.
My teacher recommended a JQuery function, but I don't have any background in that language so I don't know where to begin looking.
I'm at a complete impasse. I will update this if you guys need additional information to aid in my search.
You are looking for AJAX
You will need a HTML page with jQuery/AJAX that calls another PHP page. In that PHP page you do the DB request and then ideally return the data as JSON so that your frontend part can display it to the user.
As every one says, AJAX is the way. You can find a simple blog I did on it here.

Taking a URL and adding /?var0=val0&var1=val1 with out leaving the page and no user input

OK - Thanks for taking the time!
I am using WP on a CentOS 6 server. I have a plug in with the functions, I have a function that makes a DB call and populates relevant products based on $_GET variables, I took from one of the other project and modify it so it works! But here where I run in to the problem, I go to the main page and i have a function that gets called first, goes through the URL name and determines the categories id and then from that I need to pass that to the URL so that when the next function then calls $_GET["category_id"] and that ID is there and ready to be use and it it does its magic. (all staying on the same page no refreshing or anything)
So I need to put that on the URL as the page is being loaded and so that I can use it (Again i get the variables from a function that is doing all the work with the address for relevant info,) So how do i do it? HTML or PHP, and a straight forward way no extra installs would be nice :)
Edit 1:
So is there something then I could integrate in that would be simple and straight forward that would allow me to do a mini refresh and get the right variables in place, never used JavaScript but seams to be getting or something in php ... Ideas are welcome :)
You can with javascript and the history API
The only way you can change the url without actually redirecting the user is by using the pushState method.
e.g. open a console and copy and paste this:
var stateObj = { foo: "bar" };
history.pushState(stateObj, "changes url to stackoverflow.com/yes-you-can", "/yes-you-can");
You won't be redirected, and the url of your browser will change unless, basically, you're using IE 9 or less. You can see a demo of this on html5demos.com
OK here is how I am going to get around this problem I am having
I made a new table in the DB and then I already have a list of the Domain We are using, so then I am going to give to the customer there are three columns and they will manually enter the fields and it will be on them to manage and change what they want displayed on the webpage.
CVS import and then BAM! done! just pull $_SERVER["SERVER_NAME"] and then compare that to the domain column and done! (I will have what ever cat's and sizes they want and it will not be on us to create any complicated functions and if statements for exceptions and it is in there hands!)
Not the exact answer I wanted to get but much easier and not so much complicated :)

Is using an HTML Form the ONLY way to post a $var to $PHP_SELF?

IS there a way to post a $var to $PHP_SELF so that I can use it further down the script?
After 2 hours reading dozens of questions which helpfully appear in the sidebar to the right,
it became apparent that they pretty much all assume an HTML Form has been / will be
activated.
But psuedo~code of what I need looks more like this:
< php
$someVariable=y;
$otherVar=X;
// and the usual setup for accessing the `$_POST` of php:
$HokeyDino=`$_POST`["SendOFF"];
$SendOFF=101;
// etc. and then come to a point where I need the script to just automatically post a value
[ the lack of knowledge ]
// which if I had tha codez!
// would permit the use of that $var, $HokeyDino ...
if($HokeyDino==100){
// do stuff
}
I don't like looking foolish, but gotta ask away, because I figure I have missed learning some elementary aspect of programming, being self-taught so far, but not knowing what might be lacking makes it hard to go look productively.
Thanks very much!
EDIT // Clarification.
Wow, this is amazing. half an hour, 24 people reading the question. Blows my mind.
Right. What I have gotten done so far to give more background:
A php script which uses fopen to create on the fly another php / html page, and all the
code on the Authouring originating script, to write (a+) to the newly created temp page, the whole thing.
From a loop on the authouring page, I have code for retrieving POSTS I send TO that temp page, and that code gets written to a very temp page... then I cause the first part of the page to be written, to get placed on the Temp page, by put_contents etc.,
Next, from another loop on the Authouring page, I write code which item by item matches the things which were included in the < head > of the Temp page.
Anyhow, without graphics, it's a bit tough to explain. What I have at the point I have gotten to so far, is the newly created/assemble Temporary page, can be accessed as a WebPage,, and a button click on it, will successfully POST a value back to the originating/Authouring script.
Here's the tricky part: There isn't any way I was able to devise, to dynamically create code ON THE AUTHOURING page, to recieve POSTS from the Temp Page.
But I realized that if, in the Loops on the Authoring Page, I was able to $PHP SELF post a
string which would be the code for creating a * $Var = $ POST; to catch the values from button clicks on the TEMP page, it would work.
Critical, is that the Authoring Page, doesn't know how many buttons will be made over on the Temp Page ~ that depends on the number of items in the database, which the loops are reading and translating into code which builds the Temp Page.
So, there is no way to hard~code, on the Authouring Page, all possible code for recieving posts, but I could use one Universal $Var= $ POST[ X ] if I could generate it on the fly in the loop, on the Authoring Page.
Hence the need to write code which will $SELF POST, and have it triggered just by normal programme flow, and not the click of a button in a form.
Hmm.... clear as mud yet? :) the question still is pretty straight foreward.
Cheers!
//// Loop
Create CViewerTemp
read DB and manipulate data
Loop B
create, and write to VeryTempHead page
code which creates the top of CViewer, HEAD items
create, and write to VeryTempBody page
code which will work there, items one by one matching head items
end Loop B
Write code which is 1ne time only stuff, to begin CViewer.
then transfer the stuff from VeryTempHead page, into CViewer, kill
VeryTempHead
then transfer the stuff from VeryTempBody to CViewer, kill Very Temp Body.
Open CTempViwer, click on a Button, a value gets posted to Authouring Page.
Authouring Page doesn't recieve anything, no code to do so exists [YET! :)]
If you want to create data on the fly, but not from $_POST, you can just populate $_POST from any other source like this:
<?php
// some calculations
$_POST['my_var'] = $some_calculated_stuff;
// later in your code
if(isset($_POST['my_var'])) {
// works as if it had been posted
}
?>
Is this what you're looking to do?
It's a bit hard to follow, so I'll suggest another potential:
Have you taken a look at cURL?
I think you mean hidden input fields. You can print them with php and they will be posted to your next php script.
Please note, that the user can change the values and you shouldn't trust them.
Also, you can consider using $_SESSION. That would be the better way to solve the task.
Please note that you shouldn't use PHP_SELF because it's insecure.

OnHover run php script

Okay I'm not really sure how to approach this. I have a user-generated post board where people post, it drops down onto a list of a bunch of posts. When you click on the ID number of the post it will bring you to a separate page with just that post and the comments on the post. I want it so when you hover over the href it drops down something that tells the user there are x amount of comments on this post. This way people know if there is comments without switching pages and also being able to be able to click the href still and go to the postid page.
I assume some ajax/jquery/javascript would be used to accomplish this but since I'm fairly new to ajax and jquery I'm not certain how this would be done. Thank you!
For a hover effect, it would be better if that information was already stored on the page and just hidden. Then when the user does hover, you can just un-hide it and have it positioned where you want, and then hide it again when their mouse leaves the area. Using AJAX requests for this purpose would waste away a lot of HTTP requests for such a tiny amount of information.
Really, you could do the hover effect using pure CSS if you wanted too (I would).
Since a hover happens fairly often, I wouldn't use it as the default event to fire an AJAX-request. This would increase the HTTP-traffic enormous. See if you can fetch this information when the page is build (and put it in then) or use something else like a "preview"-button for the event.
Anyways, this would be the basic workflow if you want/need to use AJAX:
Write a PHP-script (or any other language you use) which fetches the number of comments (and what else you want to display) from the database (or where your data is stored).
This script should then be called via AJAX (with $.ajax() from jQuery for example). As the expected return-type you would then use json.
The script which fetches your data would then create an object, use PHP's json_encode()-function to encode this object to JSON and echo it out.
This JSON-object will then be available in the success-method of the ajax()-method from jQuery. Then, you can access its members (e.g. the comment-count).

Categories