Hey Stackoverflow,
I've set myself a little project to celebrate the rise of cryptocurrency (having just been stitched up by a conventional bank myself with some incredibly 'un-Christmassy' charges, the growing use of a decentralized currency was a most welcomed revelation).
Basically, I want to be able to display data on my website from the following API:
http://www.cryptocoincharts.info/v2/api/listCoins
As (at least I believe) this will then enable me to use javascript to carry out my own exchange calculations using the data found there as a base rate, which I can then turn into a form to create as an easy to use exchange calculator.
This is what I have so far...
The example PHP from cryptocoincharts:
// fetch data
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.cryptocoincharts.info/v2/api/listCoins");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$rawData = curl_exec($curl);
curl_close($curl);
// decode to array
$data = json_decode($rawData);
// show data
echo "<pre>";
foreach ($data as $row) echo $row->id." - ".$row->name."\n";
echo "</pre>";
Javascript for the exchange calculations:
<script language="JavaScript">
<!--
function goldConverter(){
document.converter.bitcoin.value = document.converter.gold.value * 0.05019370
document.converter.litecoin.value = document.converter.gold.value * 1.56379100
document.converter.peercoin.value = document.converter.gold.value * 7.52631578
}
function bitcoinConverter(){
document.converter.gold.value = document.converter.bitcoin.value * 19.92281899
document.converter.litecoin.value = document.converter.bitcoin.value * 0.03210000
document.converter.peercoin.value = document.converter.bitcoin.value * 0.00667000
}
</script>
To further clarify my intention is to enable (more or less) real-time automated update of the exchange values using the data from cyrptocoincharts, figures listed above are there for the purpose of testing.
And here is my HTML:
<form name="converter">
<table border="0">
<tr>
<td>Gold (g): </td><td><input type="text" name="gold" onChange="goldConverter()" /></td>
</tr>
<tr>
<td>Bitcoin: </td><td><input type="text" name="bitcoin" onChange="bitcoinConverter()" /></td>
</tr>
<tr>
<td>Litecoin:</td><td><input type="text" name="litecoin" onChange="litecoinConverter()" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" value="Convert" /></td>
</tr>
</table>
</form>
Summary:
If anyone could either point me in the right direction (particularly regarding the API), or help me get this to work as a whole, I would really appreciate it and give credit where due, I realize I have a lot of learning to do and this is my first post on stackoverflow, so apologies if I have broken any unwritten rules.
UPDATE:
I have recently discovered money.js which potentially solves my problem, and I am currently attempting to change the data source from OpenExchange Rates API to Cryptocoin Charts API.
UPDATE 2.0:
Now using simple_html_dom.php to scrape the HTML page as seems most straight forward method, however I am receiving the following errors:
Warning: include_once(simple_html_dom.php) [function.include-once]:
failed to open stream: No such file or directory in
/srv/disk13/1587290/www/bildungsroman.me.pn/index.php on line 22
Warning: include_once() [function.include]: Failed opening
'simple_html_dom.php' for inclusion
(include_path='.:/usr/local/php-5.3.22/share/pear') in
/srv/disk13/1587290/www/bildungsroman.me.pn/index.php on line 22
Fatal error: Call to undefined function file_get_html() in
/srv/disk13/1587290/www/bildungsroman.me.pn/index.php on line 25
My new code is as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- page content -->
<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://www.cryptocoincharts.info/v2/api/listCoins');
$result = $html -> find('name');
foreach($result as $element) {
echo $element."<br/>";
}
?>
</body>
</html>
The above PHP script should be in the simple_html_dom.php directory, or use an absolute path instead of the relative path you've used.
Related
I'm a FileMaker programmer trying to port a database across to the web using PHP their API. I've got my php page working, retrieving and displaying the correct data from my search, however I would like to filter the results on my page every time my user picks a checkbox (Apple, Microsoft etc) without hitting the submit button. I know I need to use ajax to perform this, however can I inject the ajax into this page below or am I now going to have to break down the page into various smaller files, php and js files?
Most of the samples I have found are json based, which do filtering client side. FileMaker returns an odd type array with PHP which requires further processing to get into json format. I'm ideally looking for a way to just post back the form everytime my user click on a checkbox, which I think maybe simpler if possible?
<?php require_once('../db.php');
if(isset($_REQUEST['search'][0]))
{
$find = $fm->newCompoundFindCommand('Data');
$request1 = $fm->newFindRequest('Data');
if(isset($_REQUEST['search'][1])){ $request2 = $fm->newFindRequest('Data'); }
if(isset($_REQUEST['search'][2])){ $request3 = $fm->newFindRequest('Data'); }
$request1->addFindCriterion('Company',$_REQUEST['search'][0]);
if(isset($_REQUEST['search'][1])){ $request2->addFindCriterion('Company',$_REQUEST['search'][1]); }
if(isset($_REQUEST['search'][2])){ $request3->addFindCriterion('Company',$_REQUEST['search'][2]); }
$find->add(1,$request1);
if(isset($_REQUEST['search'][1])){ $find->add(2,$request2); }
if(isset($_REQUEST['search'][2])){ $find->add(3,$request3); }
$result = $find->execute();
} else {
$request = $fm->newFindCommand('Data');
$request->addFindCriterion('Company','*');
$result = $request->execute();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
</head>
<body>
<div id="filters">
<form action="data_table.php" method="post">
<input class="category" id="check1" name="search[]" type="checkbox" value="Apple">
<label for="check1">Apple</label>
<input class="category" id="check2" name="search[]" type="checkbox" value="Google">
<label for="check2">Google</label>
<input class="category" id="check3" name="search[]" type="checkbox" value="Microsoft">
<label for="check3">Microsoft</label> <input type="submit" value="Submit">
</form>
</div>
<table border="0" class="table table-striped" width="100%">
<thead>
<tr>
<th>Company</th>
</tr>
</thead><?php if(!FileMaker::isError($result)) {?>
<tbody class="searchable">
<?php foreach($result->getRecords() as $row){ ?>
<tr>
<td><?php echo $row->getField('Company'); ?></td>
</tr><?php } ?>
</tbody><?php } ?>
</table><!-- end row -->
</body>
</html>
Let me try and break down you code part.
$request->addFindCriterion('Company','*');
$result = $request->execute();
At this point you have the results after applying the query. Just encode it in json like
echo json_encode($result);
this is your api endpoints. You will be making all ajax queries over here. Move all html content to a separate file.
Now this part of code
<table border="0" class="table table-striped" width="100%">
<thead>
<tr>
<th>Company</th>
</tr>
</thead><?php if(!FileMaker::isError($result)) {?>
<tbody class="searchable">
<?php foreach($result->getRecords() as $row){ ?>
<tr>
<td><?php echo $row->getField('Company'); ?></td>
</tr><?php } ?>
</tbody><?php } ?>
</table><!-- end row -->
becomes obsolete as you might have guessed for obvious reasons. There is no $result in this file. It is just a static html. You need to make ajax request in this file to the api point we just used above. You will get the response in json. Populate it into a table. Similarly if the users has other search parameters, make ajax request with proper search and repopulate the table in javascript.
Which part is simpler ?
That purely depends on the kind of application you are building. If it is somewhat along the lines of Single Page app i would suggest javascript filtering else go for filter in api.
Remember javascript does not have proper sql database and they are implementations of localstorage so the execution might be long, but that is a tradeoff people make for persistant apps.
I've searched in vain for days, but haven't found a solution for my problem yet.
Ideally, I would like to embed a fillable pdf form into an intranet html form for submission to the server for processing (ability to parse the field/values would be gravy, but not required). The files are all in the same domain so no cross-domain issues. I know I could add submission functionality to the pdf form itself, but 1) scripting is beyond the ability of the pdf document administrator and I don't want to take that on, 2) there are hundreds of pdf documents, 3) I need additional scripted fields/values submitted with the form, 4) I want the pdf document to be contained within the login session. So far, the server log shows all the field/values, except the PDFInput parameter which is passed, but the value is empty.
Here's what I have so far:
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(uploadForm).on("submit", function(event) {
var iframe = document.getElementById('PDFObj');
var iframeDocument = [iframe.contentDocument || iframe.contentWindow.document];
var pluginData = iframeDocument;
$(this).append('<input type="file" name="PDFInput" id="PDFInput" value="' + pluginData + '" style="visibility:hidden"/>');
return true;
});
});
</script>
and
<form enctype="multipart/form-data" method="post" name='uploadForm' id='uploadForm'>
<input type='hidden' name='rm' id='rm' value='uploadFile'>
<table align='center'>
<tr>
<td align='left'>
<strong>Notes:</strong>
<br>
<textarea cols='80' rows='2' name='notes' id='notes'></textarea>
<br>
<br>
</td>
</tr>
<tr>
<td colspan=2 align='center'>
<input type='submit'>
</td>
</tr>
<tr>
<td colspan=2>
<br>
<input type='hidden' name='formid' id='formid' value='6F45B3AF-91F3-108C-D3D9-701F541B49DC'>
<iframe type='application/pdf' src="url.pl?formid=6F45B3AF-91F3-108C-D3D9-701F541B49DC.pdf" height='800' width='1000' name='PDFObj' id='PDFObj'>
</td>
</tr>
</table>
</form>
I've tried embedding it using iframe and object along with setting input type="object", but I can't get any combination to work.
Is this even possible? Is there a better approach?
As far as I know, you're not going to be able to capture the PDF data directly from HTML like that. Your best bet is going to be to add submit functionality to the PDFs, then process the resulting FDF data with a server-side script.
You will need either add Submit a Form buttons to your PDFs, or modify the existing buttons. Make sure the form action in the PDF has #FDF after the URI (eg https://example.com/process.php#FDF).
Parsing the data server side is simple. I'm not sure what server side language you are using, but here is a PHP snippet
<?php // process.php, report the data we received
echo '<h2>GET Data</h2>';
foreach( $_GET as $key => $value ) {
echo '<p>Key: '.$key.', Value: '.$value.'</p>';
}
echo '<h2>POST Data</h2>';
foreach( $_POST as $key => $value ) {
echo '<p>Key: '.$key.', Value: '.$value.'</p>';
}
Note that a PDF only interacts with a web server properly when viewed inside of a web browser.
I do not know of a reliable way to programmatically add submit buttons to PDFs, nor do I know of a reliable conversion method. You're between a rock and a hard place here IMHO.
More info:
http://www.w3.org/TR/WCAG20-TECHS/PDF15.html
http://etutorials.org/Linux+systems/pdf+hacks/Chapter+6.+Dynamic+PDF/Hack+74+Collect+Data+with+Online+PDF+Forms/
The forums have been a huge help on this project so far. I'm looking for some guidance on the next step of my project here.
What I have is a form that feeds user submitted information into a MySQL database. This database then feeds this information to a main page displaying all of the information in the DB. What I am looking to do is add something to my form that creates a new unique URL/page when the form is submitted. I have already designed the HTML/CSS template for this page and it is designed to display only one set of information as opposed to the entire DB worth.
I am looking for some guidance as to how I can create the pages and unique URLs on the form submit. What is the best way to get this fresh information feeding from the DB immediately?
I need to somehow automatically recreate the HTML and CSS files as well on the server, this I am unfamiliar with.
EDIT: After #Jacky Cheng pointed out that this was possible without creating new versions of the HTML/CSS files I would be inclined to go about having a single HTML file on the server that is dynamic.
Thanks for any help as you guys have been great so far.
Including code for the form which I am submitting to the DB from, and the page which I will be pulling info from.
This is the form:
<?php
include_once 'post_func.inc.php';
connect();
?>
<!DOCTYPE html>
<html>
<head>
<title>Event Register</title>
</head>
<body>
<div style="text-align: center">
<h2>Event Register</h2>
<form id="eventregister"action="eventtestconnect.php" method="post">
<table style="border: 0; margin-left: auto; margin-right: auto; text-align: left">
<tr>
<td>Event Name:</td>
<td><input name="name" type="text"></td>
</tr>
<tr>
<td>Event Type:</td>
<td>
<select name="eventtype">
<?php query_eventtype() ?>
</select>
</td>
</tr>
<tr>
<tr>
<td>Venue:</td>
<td>
<select name="venue">
<?php query_venue() ?>
</select>
</td>
</tr>
</table>
<input type="submit" value="Submit">
</form>
</div>
</body>
<?php close() ?>
</html>
This is the page I want filling with information from the DB after the form is submitted and the url is generated.
<?php
include_once 'event_func.inc.php';
connect();
?>
<html>
<head>
<title>
<?php query_eventname() ?>
</title>
<link href="eventstest.css" rel="stylesheet" type="text/css"/>
</head>
<body id="body">
<div id="maincontainer">
<div id="header">
</div>
<div id="content">
<div id="eventname">
<?php query_eventname() ?>
</div>
<div id="eventvenue">
<?php query_eventvenue() ?>
</div>
<div id="eventicon">
<?php query_eventtype() ?>
</div>
</div>
</div>
</body>
<?php close() ?>
</html>
What changes need to be made to the form in order for the url to be generated on submit and the event page to be able to jump between urls/sets of data dynamically, per-say?
Sorry for the beginner questions but this site really seems to be the best resource for these sorts of things and I haven't found anything this specific on here!
Thanks again for the help!
I am still half guessing what you want, so bear with me here.
from the description of your question, you seems to have a system that would generate an actual html file per form submit? That doesn't look good to me.
maybe try something like this :
redesign a web page that would take http GET request parameter as input (mydomain.com/display.php?id={input1}) and display only 1 set of info.
from the comments I see you have a unique id per form submit, I'd suggest avoid using it directly in the request as it'll be extremly easy to get someone else's info. Instead try somthing like MD5 encoding for that id and then sending that out to user.
so the overall system would be:
1) you'll only ever have 1 html file in your server, which will dynamically change it's content according to input, which save you a lot of space.
2) you'll have a unique & slightly more secure URL per form submit
edit:
here are some fake code to show the general idea.
form response:
$uniqueId=mysql_query("SELECT unique_id FROM my_db");
echo "http://yourdomain.com/display.php?urlid=".$uniqueId;
display.php
<?php
$uniqueId=$_GET['urlid'];
mysql_query("SELECT info_you_need FROM your_tables WHERE unique_id = $uniqueId");
?>
<html><body>your display page html here</body></html>
I guess...
You want to create product catalog page like this:
www.abc.com/Electronics/Product-Motorola-moto-g-at-Rs6999-only.html
and this will display all the product information from the database.
If the above is your case then you can use url rewrite in your project.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^Product-/?$ Product-Motorola-moto-g-at-Rs6999-only.html [NC,L] # Handle requests for "Product-"
The "RewriteRule" line is where the magic happens. The line can be broken down into 5 parts:
RewriteRule - Tells Apache that this like refers to a single RewriteRule.
^/Product/?$ - The "pattern". The server will check the URL of every request to the site to see if this pattern matches. If it does, then Apache will swap the URL of the request for the "substitution" section that follows.
Product-Motorola-moto-g-at-Rs6999-only.html - The "substitution". If the pattern above matches the request, Apache uses this URL instead of the requested URL.
[NC,L] - "Flags", that tell Apache how to apply the rule. In this case, we're using two flags. "NC", tells Apache that this rule should be case-insensitive, and "L" tells Apache not to process any more rules if this one is used.
Handle requests for "Product" - Comment explaining what the rule does (optional but recommended)
Hope this will work for you.
Feel free to ask any help.
Happy programming :)
Bear with me too. Your description is pretty bad. So if I am correct, you want form=>mysql=>confirmation
So, form should be action="process.php" method="post"
Create a process.php file where you do your validation, escaping, serializing, etc. Insert into the MySQL table. If returns true redirect (header(location:yourdomain.com)) and then on the redirected page, select the information from the Database.
I'm learning about parsers and after taking a look at Java parser JSoup I'm trying to do the same with PHP. I'm using PHP Simple HTML DOM (don't now if it's the best solution) to get some data from an url, but without success. How can I take the 8 (near bar1) and 18 (near bar0) numbers from the next code from external web using a PHP parser?
<table>
<tr><td><div name='divns6' id='divns6' style='position:relative;visibility:hidden;'
width='400' height='160'><table valign=botton cellpadding='0' cellspacing='0'
border='0'><tr valign='bottom'>
<td width=15 valign="bottom" height=150><a href="javascript:void(null)"
onMouseOver="changeImage('bar1','','47',2);activadiv('bar0','18');"
onMouseOut="changeImage('bar1','','47',0);desactivadiv('bar1');"><img
NAME="barra1" width="11px" height="47" border="0"></a></td>
<td width=15 valign="bottom" height=150><a href="javascript:void(null)"
onMouseOver="changeImage('bar2','','21',2);activadiv('bar1','8');"
onMouseOut="changeImage('bar2','','21',0);desactivadiv('bar2');"><img
NAME="barra2" width="11px" height="21" border="0"></a></td>
</td></tr></table>
Here's what I've tried, but It only shows the entire line, not only the number:
<?php include ("simple_html_dom.php");
// Create DOM from URL or file
$html = file_get_html('http://www.myurl.com');
// Find all text blocks
$ret = $html->find('a[onMouseOver]');
for ($count='0'; $count<count($ret);$count++) {
echo $ret[$count];
}
?>
With JSoup the line who do the trick was: String onMouseOver = doc.select("a").attr("onMouseOver");
Thank you in advance for your time and dedication.
Finally I've found the solution on my own!!
<?php include ("simple_html_dom.php");
// Create DOM from URL or file
$html = file_get_html('http://www.myweb.com');
// Find all text blocks
$ret = $html->find('a[onMouseOver]');
for ($count='0'; $count<count($ret);$count++) {
echo split("'", $ret[$count])[9]." ";
}
?>
After looking around on a Google without any success, i feel posting here may be a good idea as I have used this site to answer previous questions.
Anyways, I am currently working on an HTML5 canvas game using; PHP, MYSQL, Html5, and JavaScript.
I have MYSQL databases setup and an PHP page displaying player high-scores, and usernames.
My question is how would I go about displaying the high-scores inside the canvas once the game is over.
As well as saving the high score when the game ends. I've looked on W3SCHOOLS site about AJAX but I'm still unsure of what codes to use inside the JavaScript file.
These are my php/script codes. or at-least the ones that are relevant:
// Here's the savescore.php file
<?php
include 'connect.php';
$user_score = ($_POST['user_score']);
$user_name = ($_POST['user_name']);
if(mysql_query("INSERT INTO users VALUES('$user_name','$user_score')"))
echo "Score Successfully Saved";
else
echo "Score Saving Failed";
?>
// Here's some of the index.php file
<link rel="stylesheet" href="css.css">
</HEAD>
<body>
<div id="menu">
<a class="item" href="/index.php">Home</a>
<?php
include 'connect.php';
session_start();
if($_SESSION['signed_in'])
{
echo 'Hello ' . $_SESSION['user_name'] . '. Not you? Sign out';
include 'chat.php';
}
else
{
echo 'Sign in or create an account.';
}
?>
</div>
<BODY>
<canvas id="canvasGAMEOVER" width="800" height="599"> </canvas>
<script src="game.js"> </script>
// here's whats inside inside game.js... well the part I want to be able to save score
var score = 0;
function drawGAMEOVER() {
}
I have used google and looked at tutorials for AJAX, I found I have been able to connect to the server using AJAX only using:
<form action="savescore.php">
user_name: <input type="text" name="user_name"><br>
user_score: <input type="text" name="user_score"><br>
<input type="submit" value="Submit">
</form>
inside the index.php page, but I am not sure if its possible to grab the 'user_name' they logged in with (displayed on the index.php page) as well as this.score (displayed inside the javascript file.)
Could anyone tell me how this is possible... if not maybe a better way of doing this?
Any help/reply is much appreciated thanks in advance.
If you are using jquery you can use $.get to get all highscores.
http://api.jquery.com/jQuery.get/