How to click a button programmatically in php - php

I'm working on a project of web scraping. Getting data from different online stores, but due to limitation of showing only a few records par page I can't get data in bulk without clicking the "show more" button at the bottom and in most cases that button is actually a span tag with just text in html mean with no achor tag. So how can I click that button in my php code. I'm using simple html dom parser php library for scraping.

Have you tried echo a script to perform click?
<?php
echo '<script>
$(document).ready(function(){
$("#btn").click();
});
</script>';
?>

Using a headless web browser like PhantomJS or SlimerJS would be a better tool for the job, because that button is surely executing some javascript event(s) and dynamically modifying the DOM.
Otherwise, try looking at the network tab in something like Chrome dev tools and examine the format of the ajax requests that fetch more data when the more button is clicked. Then, see if you can simulate that request format to fetch multiple pages from php.

Check for the id or class because most of the sites uses ajax call for the show more functionality

Related

Save HTML DOM to Server

I'm trying to develop a site where one can enter into an editor mode (with the following javascript) and change the DOM.
document.body.contentEditable = "true";
document.designMode = "on"; void 0;
I want to submit the entire HTML DOM when the user clicks a "save changes" button (via html form), and then have PHP load those changes when the Site is refreshed.
Is this possible? I am new to PHP, so I don't know if this is the right way to go.
I think most browsers now support the outerHTML on the DOM but to play it safe you should be able to do var markup = document.documentElement.innerHTML;
You are now free to send markup via AJAX or any other way you wish to your server. However you should note if you don't take the necessary steps the Save button will also be included in markup.
It might be better to split your GUI into a DIV and your content into a DIV. Then just send the content separately.

how to get what is not shown in the source of html page

I have an ajax, jQuery and PHP scripting that returns more data from a database as I scroll down (infinite scroll until more data is in the database).
The thing is that each data is placed into a div with a unique id, but only the initial content (loaded when the page is entered for the first time) appears in the HTML source.
Once the more content is displayed, none of if appears in the HTML source code.
Anyone knows how to get to the ids of the divs that are loaded on scroll? I need like an explanation of what's going on.
thx
If you're using Firefox, you can install Firebug and watch the HTML change dynamically (with your eyes!!): http://getfirebug.com/
On Chrome, you can use the built-in Developer Tools: https://developers.google.com/chrome-developer-tools/
To get the id's of the elements that are loaded via AJAX, you'll want to use jQuery's live function.
You could also capture the DOMNodeInserted event.
$(document).on('DOMNodeInserted', function(e){
console.log($(e.target));
});

PHP MYSQL Updatable Form

I am trying to create a form that you can enter data into mysql using php and then it will update the table on the same page instantly here is what I have
Index_test.php: http://pastebin.com/03fndSHG
Update.php: http://pastebin.com/jQraSskS
index_style.css: (http://)pastebin.com/PhYxttFu
When I submit this I get a double entry in my form and I want the update to be seamless like on the index_test.php it shows a line that says "An entry has been added" and the table refreshes automatically without any movement to another page. I ahve tried finding something with Ajax but nothing I try works
If I were doing this, I would use jQuery for the ajax. There's lots of documentation here: http://docs.jquery.com/Main_Page. It simplifies ajax, if you know how jQuery works. To use jQuery, you'll need to know some javascript as well. Without jQuery, just javascript & php is enough, but trickier because internet explorer does ajax differently than the other browsers.
Without ajax, you should probably submit the form to the same page as the form, which then redraws itself with the new table row. Even with ajax, if the user turns off javascript, then the form needs to submit to the same page, or another page that has the form in it. This is known as progressive enhancement or graceful degradation, meaning, the web page still works if javascript is disabled.

Scroll text down after clicking

I am having difficulties in the following problem:
I have a screen in PHP which displays a list of some records when I choose any of these
records (by clicking) it gives me a web site to share the data with this record. So far, it works.
I need to click on some of these records, instead of him
open another page, scroll down the screen and the record data to appear in the same screen, ie without opening another window.
Do you have any idea how to do this?
Thanks
You'll need to have a DIV at the bottom of the page, which will be completed by using an Ajax call and some javascript or jquery.
Without going into too much detail, heres what needs to happen:
User clicks a link which fires off an ajax request.
The backend PHP script takes the ajax call and generates either XML or pure HTML and returns the data.
JQuery or JavaScript on the original page takes the return and populates the empty DIV at the bottom of the page.
Regards
It sounds like you'll need to use ajax to pull this off.
I would personally suggest starting with reading up on the jQuery javascript library if you are not familiar with it already. It provides a very good set of ajax tools to work with.
Create a DIV layer on the bottom of the page. Use a simple AJAX library like this
Create a new php page that will only load a new record based on the recordID and call this page on the onclick method of your link that is now opening in the new window
I would try adding some jQuery to your page to handle this effect.
If you do add jQuery here is a function written to do just that:
http://pastebin.com/SeMHwSgg
Call the script like so:
Where a is the record you are having them click on and href="[some anchor]" located at the spot on the screen where you want the scrolling to stop:
<a id="gotop" href="#" onclick="goTop();return false;">click here</a>
So
Indeed, there is no error, it just does not have the effect that (scroll down the screen and show the record data). For now, it only shows the record open in another window.

HTML toggle button for simple site using php possible?

I've got a simple website using plain HTML/CSS to display and PHP/MySQL for data storage.
Now I'd like to add a toggle button similar to facebooks "like" button.
How can I act on the user pressing the button (add database record for this item, change button text) without leaving the page?
I thought this question would have been asked and diskussied to no end, but all solutions I found require some other frameworks than plain PHP as background.
You'll need to do it with javascript. Read up on "AJAX form posting".
A high level view:
user clicks on button
you capture the click via an onclick handler in javascript, and use it to call a javascript function
said function does a remote url request via XmlHttpRequest to a target page
that target page takes in the parameters passed via POST or GET and performs actions with them (eg add database record), and prints out any response required
the javascript function reads the response and acts accordingly (eg change button text)
and all this happens without refreshing the page.
You can do all this with pure low level javascript code, but plenty of libraries already abstract it while solving various issues with browser compatibilities. I'd suggest the jQuery javascript library. It provides an easy way to do exactly what you require, and good documentation.

Categories