Implementing PHP Inside a HTML Table - php

Currently, I am building a table full of content. I can sucessfuly make one row that has one cell (the first cell) saying data, however when I attempt to use php to echo something, it does not show up. Why is this?
If you need my whole program I would be happy to include it, however to stay clean I am going to only include a section of the table.
Code:
<tr>
<td>data </td>
<td><?php echo "hi"; ?> </td>
<td> </td>
<td> </td>
</tr>

What you are doing is very right.
There are two reasons why that may not be working.
1- check if your filename ends with .php
and
2- check if you are running your code from a webserver that supports php.
To know if php is installed just try to right a simple php page iwth the following content.
if it renders it means you have php installed

Related

my php file doesnt output but instead shows source file

I am trying to add a php code in my html file, but whenever i do, my file instead of outputting my expected outcome, it instead presents the source code
again, i am creating a server for my project at school and i am a total noob in programming as i learn it all by myself without formal education about it. i tried to save the file as both .html and .php, although .html presents the output, but not the desired one. now i have two files below and since i am a noob in programming, my concept was to have users input the form in the .html file and output in on an identical one but in .php
this is the first file, saved in ABM11.html
<form action="AMB11.php" method="post"><table border="0">
<tr>
<td>Name</td>
<td align="center"><input type="text" name="StudentName" size="30"></td>
</tr>
<tr>
<td>Subject</td>
<td align="center"><input type="text" name="Subject" size="30"></td>
</tr>
<tr>
<td>Final Grade</td>
<td align="center"><input type="text" name="FinalGrade" size="30"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="submit"></td>
</tr>
</table>
</form>
this is the second file, saved as ABM11.php
<?php
$studentname = $_POST['StudentName'];
$subject = $_POST['Subject'];
$finalgrade = $_POST['FinalGrade'];
echo $studentname '<\br>';
echo $subject '<\br>';
echo $finalgrade '<\br>';
?>
basically i just want users to answer a form then have that data be posted on the same page perpetually, not have my noob source code presented
it seems , the problem with your web server , weather you are using apache or nginx . its not running properly or its not configured properly on your local machine so rather then executing php code its just showing that file on the browser .
You can debug like , create one simple php file just like echo "yourname "; and put it to your www directory and try to run it form browser if the server is proper configured then it must print and if its not prited then its mistake with your local server configuration .
and also on your source code there is one mistake of echo and .
Wrong : echo $finalgrade '<\br>';
True : echo $finalgrade .'<\br>';
You must concat br with .
that is mistake 110% but as you said in your question the code is showing on the page is not only because of this , its surely configuration problem
if . is the only mistake then it wont show you the source code on the page but you will get any php error if thats the only issue .so check your config well first.
Thanks
Akshay Champavat
If you combine variables and strings in an echo, you need to add dots to connect them. So instead of this
echo $studentname '<br>';
you need to write it as
echo $studentname.'<br>';
And similar in all other lines.
(and no backslashes, by the way!)
And also ("sourcecode displayed...") make sure to open/call the php file via a server (for example XAMPP, via localhost), not simply as a file on your computer.

Use PHP Simple HTML Dom Parser to retrieve partial id and use the result to parse another URL?

To learn some html, css and php I decided to create a simple page to show football scores. I'm using PHP Simple HTML DOM Parser to retrieve the football scores and league table from this BBC webpage which was simple enough however what I would like to do now is retrieve goal scorers. Unfortunately I'm unable to parse this information as the page doesn't contain it until the fixture is clicked and the table expanded.
I noticed that each table row has the following ID:
<tr id="match-row-EFBO803510" class="report">
<tr id="match-row-EFBO803454" class="report">
<tr id="match-row-EFBO803452" class="report">
After some fiddling about I found out that the string at the end of the ID that begins with EFBO is the fixture ID. This ID can be added to the end of a url and it'll take me to a page with the information I'm looking for like so:
http://www.bbc.co.uk/sport/football/live/partial/EFBO803510
So what I'm hoping to do is parse the site for those table rows, retrieve the fixture IDs and add each to the end of bbc.co.uk/sport/football/live/partial/ to generate a new url to parse.
Is this possible? Sorry I don't have any example code, everything I tried was throwing up php errors. This is an example of what the table looks like on the BBC website:
<tbody>
<tr id="match-row-EFBO804038" class="report">
<td class="match-score">
<button class="hidden-text">Show stats panel</button>
<span class="team-home">Brighton</span>
<span class="score">5 - 0</span>
<span class="team-away">Fulham</span>
<span class="elapsed-time">Result</span>
</td>
<td class="match-link">
Report
</td>
</tr>
<tr id="match-panel-EFBO804038" class="panel-showing live">
<td class="details-table-container" colspan="2"></td>
</tr>
<tr class="row-spacer">
<td colspan="2"> </td>
</tr>
</tbody>
As an example, that first link: http://www.bbc.co.uk/sport/football/live-scores
This seems to be the site you are DOM parsing, and has <tr id="match-row-EFBO851386" class="fixture">, with that EFBO851386 only repeated twice, once there and once right below.
So my recommendation is that you would want to parse for the first TR with CLASS 'fixture' and replace("match-row-",$val) to retrive the id.
That can be appended to url as you mentioned: "http://www.bbc.co.uk/sport/football/live/partial/".$id
How you add the return of that url to your generated page depends entirely on how your adding the initial info.
I would usually use ajax and append below your initial html response, nice spinner while it does that.
But you can retrieve it before returning initial html response, really depends on how you actually parsing and how you using returned content.

Html - how to make the browsing button open in a desired path

I have this html code(+some backdoor php codes) to delete images from my website, but the problem is that I need that browsing dialoge to be opened in a specific path and be stuck on that path, I like I don't want the user to be able to navigate through the folders as he likes, is that possible to be done in any ways?
<table>
<tr>
<td>Select a project : </td>
<td><input type='file' name='userFile'><br></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="upload_btn" value="delete"></td>
</tr>
</table>
thanks
No, it isn't … and it shows the user's local file system rather then the servers anyway.
Write some server side code to generate a list of files that could be deleted and generate an HTML document listing them instead.

Get certain content from a html page

When I press 'View source code' of a certain web page, it's kind of like this:
<form action="/WANem/index-advanced.php" method="post">
<table border="1" width="100%">
<tr>
<td width="10%" >Delay time(ms) </td>
<td width="10%" ><input type="text" name="txtDelay1" size="7" value=1200>
</td>
<input type="submit" value="Apply settings" name="btnApply">
</table>
</form>
My question is: How can i get '1200' in the code using PHP.
I mean i just want to get a certain string in the html code of another website without having to press 'view source code' and copy that string.
Thanks for any reply.
What you're trying to do is called "web scraping".
Here's a StackOverflow question with a bunch of helpful answers:
How to implement a web scraper in PHP?
And here is a tutorial that probably explains it better than I could by typing it out here:
http://www.thefutureoftheweb.com/blog/web-scrape-with-php-tutorial
Hope it helps and good luck!
In your php file it is something as simple as this:
$value = $_POST['txtDelay1'];
Although, it looks as a really basic question. I suggest you to go through some tutorials, to get the idea on how it all works.
First on in google php form tutorial: http://www.phpf1.com/tutorial/php-form.html
EDIT:
Oh, now i see your edits. In that case, you can't skip sending a http request to get the source code, just like a browser does. Next, you have to parse the response from the server, just like a browser does as well. Ah, The response will be the "Source code" you're asking for. If you can, consider using python to this. It will be much more faster and efficient.
If PHP is a must, be aware that this task is a pain in the ass;)
You can do this with file_get_contents() and preg_match().
//$url is whatever your URL is
$url = file_get_contents($url);
preg_match('|name="txtDelay1".*?value=([\d]+)|', $html, $html);
echo $html[1];
//should print your value 1200
Check out the regex here.
However
This is only going to work as long as this code appears exactly and is not duplicated. Also, if you are scraping from another site, it could be changed by the owner, and the regex would no longer work.

Problems with Dynamic fom with JQuery and PHP

I'm doing an Invoice system, and i'm facing a problem.
Imagine a simple Invoice, where you have rows (lines, tuples, whatever) where you place the thing you're buying, the amount, the price, etc.
So, in my script i've a form like this:
<form action='I-process-the-form-but-not-see-data.php'>
<input ... //some other imputs (client,date,etc)
<table>
//table hedaers
<tr>
<td><input type='text' name='invoiceData[0][name]' /></td>
<td><input type='text' name='invoiceData[0][amount]' /></td>
<td><input type='text' name='invoiceData[0][price]' /></td>
</tr>
</form>
I provide a button that adds a line to the invoice. I'm doing it with JQuery.
I first get the first and clone it. Replace invoiceData[0] with invoiceData[COUNTER] (Global counter) and everything looks great.
The problem is that my data is not being sent to the php script. The only data i get is the inputs that are static (first loaded). Not the one that i add with jquery.
I've inspect it with firebug and it's adding the new lines ok.
Actually, if i paste the code generated by JQuery in the static form it sends it well.
What could be happening?
EDIT (JQUERY MAGIC):
var clon = $("#selectorForTR").clone().html(); //there is a minimal workaround for this but doesn't matters
clon = $(html.replace(/invoiceData\[0\]/ig, "invoiceData["+contador+"]"));
$("#tableBody").append(clon);
Make sure you are opening and closing your cloned tr, i think that is why it is only getting one row.
<tr> ...html... </tr>
See my example : http://jsfiddle.net/wADef/
Changed it a bit to work in jsFiddle, but check it out and let me know.
Are you certain that you are appending the generated html in the right place? ie, inside the form tag? If the data isn't coming through, that's the only thing I can think of immediately! :]

Categories