i have a page,say abc.html, that is having a small form with some fields.
<form name="form" method="post" action="abc.html">.......................</form>
when we submit the form it again comes back to abc.html with some data posted and shows the resulted names on the page which came after processing the posted data.
in the whole procedure the page url remains same.Now i want to parse this abc.html containing data after the submission of the form.I have done parsing in which the original url contains all the data but not like this on which after submission the data gets displayed on the page.Please tell me how can i parse such page??
Well, to get the correct HTML from the server, you have to send a POST request containing the form data. Then you can parse the server response.
Parsing the HTML file is same as us seeing it. So the HTML page rendered after posting the data will have some or any HTML element in which the additional text is displayed. When you parse the page chek if this or a container exists if so then read the rest of the data. The HTML page displayed without the posted data will not have this additional or container.
Edit: Look at this question : PHP Screen Scraping and Sessions
First of all. Your page should be abc.php. Otherwise it will not parse any php.
Second. Here is some code that will help you out (I Hope). Copy/Paste this example and place it in abc.php
<html>
<head></head>
<body>
<?php
if (isset($_POST['submit'])) {
echo 'you posted the following value :'.$_POST['foo'];
}
?>
<form name="form" action="abc.php" method="post">
<input type="text" name="foo" value="" />
<input type="submit" name="submit" value="Press Me" />
</form>
</body>
</html>
If this is not the case. And you want to parse HTML like parsing XML you should use the DOMDocument class of PHP
$oDom = new DOMDocument();
$oDom->loadHTML($sHTMLstring);
// or
$oDom->loadHTMLFile($sFileName);
// now you can walk the dom like
$oDomElement = $oDom->getElementByTagName('form');
http://nl.php.net/manual/en/domdocument.loadhtml.php
http://nl.php.net/manual/en/domdocument.loadhtmlfile.php
http://nl.php.net/manual/en/domdocument.getelementsbytagname.php
Hope this helps
Good question, but I think it's not possible with PHP. My company doing that with very advanced tool in C. It just grab any page and send the any form and get rsponse HTML.
But You can found maybe some tools. Don't know.
I think the point here is that you can't just open the URL and read the HTML that comes back. You will have to play the part of the browser in order to interact with the server side form. To do this, you'll have to write your own code to HTTP POST the form input data. The HTTP response to your POST will contain the generated HTML, which you can then parse for the processed results.
If you want to send the form to the web server (i.e. "fill" it first) you need something similar to Perls WWW::Mechanize. See this question for possible solutions to do this. Afterwards, you need to parse the resulting page, and that heavily depends on the site in question itself: one site might use named elements you can easily retrieve using regular expressions, a different site might not, making it much harder to get the values you're interested in.
Related
Beginner's question here :
file #1 : file1.html (one dataset)
//header, body and some other stuff
<form action="some_logic.php">
<input type="text" name="data1" placeholder="data1">
<input type="text" name="data2" placeholder="data2">
<input type="submit" value="Ok!">
</form>
//end of the html
file #2 : (I don't even know what this file should be : php? html? js?)
//usual stuff
include 'file1.html'; //OR
<!--#include file="file1.html"-->
#logic to replicate file1.html with a button
I need to have the file1's code repeated once each time the user clicks on a button (or a link or whatever) and I would like this to work without refreshing the page (or at least without questionning the server over and over and without deleting the current datas). The server uses php7.
I need this because I do not know in advance how much datasets the user will create.
It would be awesome if this could be done with javascript! (since I doubt this can be achieved with php/html only) Although I have never used js until now.
You can fetch data from server without refreshing the webpage using JavaScript Ajax. Here you can create an event listener to send a request to the server. The data can be plain text, html, json, xml or whatever you want. Just create Ajax request for events. Then update the webpage using the response data.
Take, for example:
<form action="/action_page.php" method="get">
#Code here...
</form>
The action attribute results in a submit sending the form data to a PHP file called action_page.php.
Is it possible to refer it to an HTML file instead?
Like, maybe:
<form action="/action_page.html" method="get">
#Code here...
</form>
I realize that this may be too simple a question. But I really just want to know if it is possible.
It not, is there any other way to send form data from HTML file #1 to HTML file #2 so that I may make use of the data from file #1 in file #2?
Note:
I don't want to manipulate my HTML file to run or function as a PHP file unless it is the best way to be able to transfer form data from one HTML file to another.
The action has to point to a URL.
If you make a POST request, then the submitted data will be available only to server-side code (browser-side JavaScript cannot access it).
HTML is not a programming language. It cannot do anything with a POST request. Server-side code can dynamically generate HTML as its output though.
The server-side code can be written in PHP. It does not have to be. You can use any programming language you like.
If you use PHP, then you do not have to have .php in the URL but that is the simplest way to configure a server to run a PHP program to generate the response to a request for a particular URL.
I’m trying to store the content of a div to a variable.
Example:
<div class="anything">
<p>We don't know the content of this div</p>
</div>
I want to search for <div class="anything"> and store everything between opening and the end tag.
We also want to avoid using absolute pathnames, so that it only searches the current HTML/PHP file for this div where the code is present.
Is this possible with PHP, or is this only possible with JavaScript ?
PHP is not that intelligent. He doesn't even know what he says.
PHP is a server-side language. It has absolutely NO clue about what the DOM (ie. what is displayed in your browser's window) is when it delivers a page. Yeah I know, PHP rendered the DOM, so how could it not know what's in there?
Simply put, let's say that PHP doesn't have a memory of what he renders. He just knows that at one particular moment, he is delivering strings of characters, but that's all. He kind of doesn't get the big picture. The big picture goes to the client and is called the DOM. The server (PHP) forgets it immediately as he's rendering it.
Like a red fish.
To do that, you need JavaScript (which is on the client's computer, and therefore has complete access to the rendered DOM), or if you want PHP to do this, you have to retrieve an full-rendered page first.
So the only way to do what you want to do in PHP is to get your page printed, and only then you can retrieve it with an http request and parse it with, in your case, a library such as simpleHtmlDom.
Quick example on how to parse a rendered page with simpleHtmlDom:
Let's say you know that your page will be available at http://mypage.com/mypage.php
$html = file_get_html('http://mypage.com/mypage.php');
foreach($html->find('div.anything') as $element)
echo $element->src . '<br>';
you probably need a combination of those.
In your Javascript:
var content = document.getElementsByClassName("anything")[0].innerHTML();
document.getElementByID('formfield').value(content);
document.getElementByID('hiddenForm').submit();
In your HTML/PHP File:
<form id="hiddenForm" action="path/to/your/script">
<input type="hidden" name="formfield" value="" />
</form>
In the script you defined in the form action:
if(!empty($_POST)){
$content = $_POST['formfield'];
// DO something with the content;
}
Alternatively you could send the data via AJAX but I guess you are new to this stuff so you should start slowly :)
Cheers!
steve
You could use JS to take the .innerHTML from the elements you wan and store them in .value of some input fields of a form and then use a submit button to run the PHP form handling as normal. Use .readOnly to make the input fields uneditle.
I have a php with a form/text area, I do not want to use a button press to post data to some other PHP.
I am looking for some auto_post AJAX query which will put all the data that user has typed in the text box, into a $variable in the same php.
Something like this:
<?php
$checkbox.= '<input type="textbox" name="vehicle" value="" />';
echo "$checkbox<br>";
$return = $checkbox;
//$return should have all the data typed by the user
?>
This is not possible without PHP as it is a serverside language, which means, that all code is processed before the document is sent to the Browser. Ajax and JavaScript however are Clientside languages, which first come to work when the Client recieved the document.
Working with php asynchronus can be reached as you already guessed by using Ajax. By sending information to a php script and printig it as a xml document you can run what you want to progress and read it on the original page again using Javascript.
But i suggest to read a tutorial on Ajax itself to get an idea of what to do, as this is a bit too much for a single Stackoverflow answer.
Here are some tutorials that might help you:
http://killerajax.com/
https://developer.mozilla.org/
Im trying to create a grid on my page in each cell there will be a simple one line form. If a person enters data into lets say FieldA I would like the php to perform actionA but if the data was entered in FieldF I would like actionF performed. Is this possible without having to create a php for each cell and upload all those php files?
Or is there a way to perform the GET method in each form to append the data to the end of the action url without the field name showing (ie sample.com/somestuff/fieldA instead of sample.com/somestuff/fieldname=fieldA) thus not needing php at all?
Did you try anything. Please try to write some code. If you get struck paste the code here, somebody will help you out..
In my opinion, why you need different forms. Just have a form which has n text boxes and perform the task that you need.
Your problem is a little ambiguous to me. I'll give it a shot though.
On the form I would set:
`method="post" action="<?php echo $PHP_SELF ?>"`
This will cause the form to submit back to itself. Then at the top of the page you could do something like the following:
<?php
if (isset($_POST["fieldA"]){
performActionA();
} else if (isset($_POST["fieldB"]){
performActionB();
}
etc...
?>
Is that what you are trying to do? Keep in mind php is executed server side before any interaction with the user.
otherwise you could use javascript to change the action field of the form. (Untested)
<script type="text/javascript">
function setAction(elt){
var page = document.getElementById(elt).value;
document.myform.action="sample.com/somestuff/"+page;
}
</script>
<form id="myform" action="sample.com/somestuff/">
<input type="text" name="text1" onchange="setAction('text1')" />
</form>