How can i add info to url in php? - php

I have a site in html and in a page, I have buttons to take the visitors to dj's profiles.
Each button takes the visitor to a different dj profile.
I create a page in html that goes to a XML document to get the information of the dj. So, my question is, how can i add at the link of php page, the name of dj, so he can stay with a personal link?
I tried the get properties but i need to have the post too, in order for PHP to take the information of that dj.
The html of djspace.html page have this:
<form name="form" id="form" method="post" action="allProfiles.php">
<input type="submit" name="dj_name" value="Blowdrop"/>
</form>
...
<form name="form" id="form" method="post" action="allProfiles.php">
<input type="submit" name="dj_name" value="Psychokiller"/>
</form>
and then my allprofiles.php page:
<?php
$counter = 0;
$dj_name = ($_POST['dj_name']);
$xml = simplexml_load_file("Profiles.xml");
foreach ($xml as $newprofile){
if($xml->newprofile[$counter]->Anome == $dj_name){
$Anome = $xml->newprofile[$counter]->Anome;
$nacionalidade = $xml->newprofile[$counter]->nacionalidade;
$naturalidade = $xml->newprofile[$counter]->naturalidade;
$residencia = $xml->newprofile[$counter]->residencia;
$emprego = $xml->newprofile[$counter]->emprego;
$generos = $xml->newprofile[$counter]->generos;
$disponibilidade = $xml->newprofile[$counter]->disponibilidade;
$partilha = $xml->newprofile[$counter]->partilha;
$sitios = $xml->newprofile[$counter]->sitios;
$tempo = $xml->newprofile[$counter]->tempo;
$editora = $xml->newprofile[$counter]->editora;
$promotora = $xml->newprofile[$counter]->promotora;
$influencias = $xml->newprofile[$counter]->influencias;
$fblink = $xml->newprofile[$counter]->fb;
$scloud = $xml->newprofile[$counter]->scloud;
$mail = $xml->newprofile[$counter]->email;
$img = $xml->newprofile[$counter]->foto;
}
$counter = $counter + 1;
}
?>
I can get all the information but I if i apply the get, I can't.
Obviously if you go directly to php page, you get none information.
http://roundhillevents.com/allProfiles.php
However go in this link, and then, dj's, dj space, and then click in the one you want to see the information.

As #Marc B suggested, use link instead of form with button.
If you want to keep using buttons for whatever reason, change method="post" to method="get". Then browser adds the dj reference to the URL of the page for you.
Of couse, you then need to use $_GET['dj_name']) instead of $_POST['dj_name'] in your PHP code. This is for both cases, buttons with GET method and links.

this is because you are using $_POST in php. try using $_REQUEST or $_GET global variable instead of $_POST
try this
<?php
$counter = 0;
$dj_name = ($_REQUEST['dj_name']);
$xml = simplexml_load_file("Profiles.xml");
foreach ($xml as $newprofile){
if($xml->newprofile[$counter]->Anome == $dj_name){
$Anome = $xml->newprofile[$counter]->Anome;
$nacionalidade = $xml->newprofile[$counter]->nacionalidade;
$naturalidade = $xml->newprofile[$counter]->naturalidade;
$residencia = $xml->newprofile[$counter]->residencia;
$emprego = $xml->newprofile[$counter]->emprego;
$generos = $xml->newprofile[$counter]->generos;
$disponibilidade = $xml->newprofile[$counter]->disponibilidade;
$partilha = $xml->newprofile[$counter]->partilha;
$sitios = $xml->newprofile[$counter]->sitios;
$tempo = $xml->newprofile[$counter]->tempo;
$editora = $xml->newprofile[$counter]->editora;
$promotora = $xml->newprofile[$counter]->promotora;
$influencias = $xml->newprofile[$counter]->influencias;
$fblink = $xml->newprofile[$counter]->fb;
$scloud = $xml->newprofile[$counter]->scloud;
$mail = $xml->newprofile[$counter]->email;
$img = $xml->newprofile[$counter]->foto;
}
$counter = $counter + 1;
}
?>

Related

PHP - Obtain data from an external script and use it in the current form

I have created a script that will work as a plugin for Wordpress whose purpose is to get the data of the videos of a page (video title, video url, thumb url and total videos found).
My plugin works but the script is on the same page where the results are loaded so that it stays in Wordpress. But I want to externalize the script in charge of doing the search because I sincerely do not want them to be able to visualize my php source code.
So I tried to separate my script and just put the html and invoke the script using the "action" form.
But I do not know how to pass the loops of my external script to the local form nor how to pass the values of the variables. I tried to use the "return" and "Header Location" but it does not work.
Here is what I currently have:
Index.php:
<?php
if (isset($_POST['search'])){
$url = $_POST['keyword'];
$parse = "http://example1.com/?k=".$url;
$counter = 0;
$html = dlPage($parse); //dlPage is a function that uses "simple_html_dom"
include("form_results_footer.php"); // I include the header of the page with the results (this part is outside the loop because I do not want it to be repeated).
foreach ($html->find('something') as $values) {
//Here I run a series of searches on the page and get the following variables.
$counter++;
$title = //something value;
$linkvideo = //something value;
$thumburl = //something value;
include("form_results.php"); //The results of the "foreach" insert them into a separate php in "fieldsets".
}
$totalvideos = $counter;
include("form_results_footer.php"); //Close the form results
} else {
?>
<html>
<form action="" method="post">
<input id="keyword" name="keyword" type="text">
<button id="search" name="search">Search</button>
</form>
</html>
<?php
}
?>
Ok, the code above works fine but I need to outsource the part where I get the variables of the part where I will receive them, something like this:
-> http://example.com/script.php
<?php
if (isset($_POST['search'])){
$url = $_POST['keyword'];
$parse = "http://example.com/?k=".$url;
$counter = 0;
$html = dlPage($parse); //dlPage is a function that uses "simple_html_dom"
foreach ($html->find('something') as $values) {
//Here I run a series of searches on the page and get the following variables.
$counter++;
$title = //something value;
$linkvideo = //something value;
$thumburl = //something value;
}
$totalvideos = $counter;
return $title;
return $linkvideo;
return $thumburl
}
?>
index.php
<html>
<form action="http://example.com/script.php" method="post">
<input id="keyword" name="keyword" type="text">
<button id="search" name="search">Search</button>
</form>
</html>
The loop results would have to be collected on the same page in the same way as in the initial example.
I hope you let me understand, thank you in advance.
Change form action url to self page in index.php and do cURL there which calls your another domain where logic is placed. that mean http://example.com/script.php
index.php
<html>
<form action="" method="post">
<input id="keyword" name="keyword" type="text">
<button id="search" name="search">Search</button>
</form>
</html>
<?php
if (isset($_POST['search'])) {
//your URL
$url = "http://example.com/script.php?keyword=" . $_POST['keyword'];
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);
// Execute
$result = curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
}
?>
And in http://example.com/script.php you just need to change $_POST['keyword'] to $_GET['keyword'] and few more change to return data.
scrip.php (from another domain)
<?php
if (isset($_GET['keyword'])) {
$url = $_GET['keyword'];
$parse = "http://example.com/?k=" . $url;
$counter = 0;
$html = dlPage($parse); //dlPage is a function that uses "simple_html_dom"
$return = array(); //initialize return array
foreach ($html->find('something') as $values) {
//Here I run a series of searches on the page and get the following variables.
$return['video_data'][$counter]['title'] = //something value;
$return['video_data'][$counter]['linkvideo'] = //something value;
$return['video_data'][$counter]['thumburl'] = //something value;
$counter++;
}
$return['total_video'] = $counter;
echo json_encode($return); //return data
}
?>
I hope this is what you want.

How to pass a big amount of parameters from PHP to a Javascript Function?

I'm developing a Joomla module, with several parameters to define how it works. I have buttons in the module area, and buttons have to interact with those parameters. Here is the module PHP code:
<?php
defined('_JEXEC') or die('Direct Access to this location is not allowed.');
$AdVisualV2jMODPHP_ver='0.3.0070';
$Station_ID = $params->get('Station_ID');
$Verbose = $params->get('Verbose');
$Bk_ColorODD = $params->get('Bk_ColorODD');
$Bk_ColorEVEN = $params->get('Bk_ColorEVEN');
$Ink_ColorODD = $params->get('Ink_ColorODD');
$Ink_ColorEVEN = $params->get('Ink_ColorEVEN');
$PicCol_Title = $params->get('PicCol_Title');
$TxtCol_Title = $params->get('TxtCol_Title');
$EvPic_Heightpx = $params->get('EvPic_Heightpx');
$EvPic_Widhtpx = $params->get('EvPic_Widhtpx');
$CatPic_Heightpx = $params->get('CatPic_Heightpx');
$CatPic_Widhtpx = $params->get('CatPic_Widhtpx');
$Pic_Hspanpx = $params->get('Pic_Hspanpx');
$Pic_Vspanpx = $params->get('Pic_Vspanpx');
$TitleSw = $params->get('TitleSw');
$MenuPos = $params->get('MenuPos');
$Paging = $params->get('Paging');
$Sort_Field = $params->get('Sort_Field');
$Sort_Order = $params->get('Sort_Order');
$db_host = "localhost";
$db_user = "xxxx";
$db_database = "xxxxx";
$db_password = "xxxxx";
$db_tabconfig = "xxxxxxx";
$db_tabpreroll = "xxxxxxxxxxx";
echo 'AVVIAMO LA PROCEDURA 0.0.090 - '.$Station_ID.'<br><br>';
echo '<div id="TabellaEventi"></div>';
echo '<INPUT Type="BUTTON" VALUE="Avanti" ONCLICK="avanti()"> ';
echo '<INPUT Type="BUTTON" VALUE="Indietro" ONCLICK="indietro()"> ';
echo '<br>';
?>
When the user will click on the "Avanti" or "Indietro" buttons i call the JAVASCRIPT functions avanti() and indietro(), and those two functions will work with ALL the parameters above. How can I do to transfer this huge amount of variables?
AND!!! The two Javascript functions will have to call OTHER PHP files to interact with a MySql database, and again i will need more or less ALL the variables in the other file.
Internet programming is such a mess sometime...
Ajax might not needed. Put all your variable in a big array of data
$data['Station_ID'] = $params->get('Station_ID');
$data['Verbose'] = $params->get('Verbose');
...
$data = json_encode($data);
$data = htmlentities($data);
...
echo '<INPUT Type="BUTTON" VALUE="Avanti" ONCLICK="avanti(' . $data . ')"> ';
Now avanti contains an JSON object as 1st parameter
Edit: because the JSON string might contain " and < you must call htmlentities to protect your string
you can use JSON + jQuery.
Q1:
create a global JS varible first.
var params = {"Station_ID":"","":"",......}
Q2:
use ajax call with jQuery.
var queryURL = 'your php file';
$.ajax({
type:'GET',
url:queryURL,
data: params// params in Q1
}).done(function(jsonObj){
dosomething();
});

parse a url, get hash value, append to and redirect URL

I have a PHP foreach loop which is getting an array of data. One particular array is a href. In my echo statement, I'm appending the particular href onto my next page like this:
echo 'Stats'
It redirects to my next page and I can get the URL by $_GET. Problem is I want to get the value after the # in the appended URL. For example, the URL on the next page looks like this:
stats.php?url=basket-planet.com/ru/results/ukraine/?date=2013-03-17#game-2919
What I want to do is to be able to get the #game-2919 in javascript or jQuery on the first page, append it to the URL and go to the stats.php page. Is this even possible? I know I can't get the value after # in PHP because it's not sent server side. Is there a workaround for this?
Here's what I'm thinking:
echo 'Stats';
<script type="text/javascript">
function stats(url){
var hash = window.location.hash.replace("#", "");
alert (hash);
}
But that's not working, I get no alert so I can't even try to AJAX and redirect to the next page. Thanks in advance.
Update: This is my entire index.php page.
<?php
include_once ('simple_html_dom.php');
$html = file_get_html('http://basket-planet.com/ru/');
foreach ($html->find('div[class=games] div[class=games-1] div[class=game]') as $games){
$stats = $games->children(5)->href;
echo '<table?
<tr><td>
Stats
</td></tr>
</table>';
}
?>
My stats.php page:
<?php include_once ('simple_html_dom.php');
$url = $_GET['url'];
//$hash = $_GET['hash'];
$html = file_get_html(''.$url.'');
$stats = $html->find('div[class=fullStats]', 3);
//$stats = $html->find('div[class='.$hash.']');
echo $stats;
?>
What I want to be able to do is add the hash to the URL that is passed on to stats.php. There isn't much code because I'm using Simple HTML DOM parser. I want to be able to use that hash from the stats.php URL to look through the URL which is passed. Hope that helps...
Use urlencode in PHP when you generate the HREFs so that the hash part doesn't get discarded by the browser when the user clicks the link:
index.php:
<?php
include_once ('simple_html_dom.php');
$html = file_get_html('http://basket-planet.com/ru/');
echo '<table>';
foreach ($html->find('div[class=games] div[class=games-1] div[class=game]') as $games){
$stats = $games->children(5)->href;
echo '<tr><td>
Stats
</td></tr>';
}
echo '</table>';
?>
Then on the second page, parse the hash part out of the url.
stats.php:
<?php
include_once ('simple_html_dom.php');
$url = $_GET['url'];
$parsed_url = parse_url($url);
$hash = $parsed_url['fragment'];
$html = file_get_html(''.$url.'');
//$stats = $html->find('div[class=fullStats]', 3);
$stats = $html->find('div[class='.$hash.']');
echo $stats;
?>
Is this what you're looking for?
function stats(url)
{
window.location.hash = url.substring(url.indexOf("#") + 1)
document.location.href = window.location
}
If your current URL is index.php#test and you call stats('test.php#index') it will redirect you to index.php#index.
Or if you want to add the current URL's hash to a custom URL:
function stats(url)
{
document.location.href = url + window.location.hash
}
If your current URL is index.php#test and you call stats('stats.php') it will redirect you to stats.php#test.
To your comment:
function stats(url)
{
var parts = url.split('#')
return parts[0] + (-1 === parts[0].indexOf('?') ? '?' : '&') + 'hash=' + parts[1]
}
// stats.php?hash=test
alert(stats('stats.php#test'))
// stats.php?example&hash=test
alert(stats('stats.php?example#test'))

Creating variables dynamically with PHP

I'm working on a REST styled API, and I want to be able to break the URL down into individual variables.
Say I have the following URL: www.example.com/user/post/1
I'd like to make the following variables:
$uri_1 = user
$uri_2 = post
$uri_3 = 1
I tried to do this but it got stuck in a loop
$path = explode('/', $this->path($uri));
for($i=0;$i < count($path);$i++){
$uri_.$i = $path[i];
}
$url = explode('/', strtolower(trim($_SERVER['REQUEST_URI'], '/')));
$uri_1 = isset($url[0])?$url[0]:'';
$uri_2 = isset($url[1])?$url[1]:'';
$uri_3 = isset($url[2])?$url[2]:'';
Here's how you do it for an arbitrary number of variables, using PHP's variable variables feature:
$path = explode('/', $this->path($uri));
for($i=0;$i < count($path);$i++){
${"uri_".$i} = $path[i];
}

Use php for Output Buffering and jQuery to send ob_get_contents

I am trying to capture the contents of my php page using output buffering:
<?php
function connect() {
$dbh = mysql_connect ("localhost", "user", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db("PDS", $dbh);
return $dbh;
}
session_start();
if(isset($_SESSION['username'])){
if(isset($_POST['entryId'])){
//do something
$dbh = connect();
$ide = $_POST['entryId'];
$usertab = $_POST['usertable'];
$answertable = $usertab . "Answers";
$entrytable = $usertab . "Entries";
$query = mysql_query("SELECT e.date, q.questionNumber, q.question, q.sectionId, a.answer FROM $answertable a, Questions q, $entrytable e WHERE a.entryId = '$ide' AND a.questionId = q.questionId AND e.entryId = '$ide' ORDER BY q.questionNumber ASC;") or die("Error: " . mysql_error());
if($query){
//set variables
$sectionOne = array();
while($row=mysql_fetch_assoc($query)){
$date = $row['date'];
$sectionOne[] = $row;
}
}else{
//error - sql failed
}
}
?>
<?php
ob_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script src = "jQuery.js"></script>
<script>
$(document).ready(function(){
$("#export").click(function(e){
//post to html2pdfconverter.php
$("#link").val("<?php echo(ob_get_contents()); ?>"); //THIS DOESN'T WORK
$("#nm").val("Entry Report.pdf");
$("form#sendanswers").submit();
});
});
</script>
<title>Personal Diary System - Entry Report - <?php echo($date); ?></title>
</head>
<body>
<h1>Entry Report - <?php echo($date); ?></h1>
<div id = "buttons">
<form id = "sendanswers" name = "sendanswers" action="html2pdfconverter.php" method="post">
<input type = "hidden" name = "link" id = "link" value = "">
<input type = "hidden" name = "nm" id = "nm" value = "">
<input type = "button" name = "export" id = "export" value = "Export As PDF"/>
</form>
</div>
<h3>Biological Information</h3>
<?php
echo('<p>');
$i = 0;
foreach($sectionOne as &$value){
if($i == 1 || $i == 3){
$image = "assets/urine".$i.".png";
echo("<br/>");
echo($value['question']." <br/> "."<img src = \"$image\"/>");
echo("<br/>");
}else{
echo($value['question'].' : '.$value['answer']);
}
echo("<br/>");
$i++;
}
echo('</p>');
?>
</body>
</html>
<?php
}
$contents = ob_get_contents(); //THIS WORKS
ob_end();
?>
I assign the contents of ob to $contents using ob_get_contents(); This works, and echoing $contents duplicates the html page.
However, in my jQuery, I am trying to assign this to a hidden text field ('link') using:
$("#link").val("<?php echo($contents); ?>");
This doesn't work however..And I have a feeling its because I am accessing $contents too eraly but not too sure...any ideas?
$("#link").val("<?php echo(ob_get_contents()); ?>"); //THIS DOESN'T WORK
at the point you do that ob_get_contents call, you've only output about 10 lines of javascript and html. PHP will NOT reach back in time and magically fill in the rest of the document where you do this ob_get_contents().
You're basically ripping the page out of the laser printer the moment the page starts emerging, while the printer is still printing the bottom half of the page.
I fail to see why you want to embed the contents of your page into an input field. If you want to somehow cache the page's content in an input field, you can just use JS to grab the .innerHTML of $('body').
Well, you have two problems.
The first is what you suspect. You can't access that stuff until later. The second problem which you may not realize is that you will have quoting issues in JavaScript even if you manage to find a way to reorder this and make it work. It's recursive, in a bad way.
What you should do instead is change your $('#export').click handler to do an Ajax call, render the HTML you need to appear in the link on the server in a separate PHP script (no output buffering necessary) and then have your code inject the result of that call into the page the way you're trying to do in your click handler now.

Categories