Run PHP code only when user clicks - php

I have a PHP file in which I have this code:
$descText = $_POST["fname"];
$attachement = array('access_token'=>$fbme['access_token'], 'message' => $descText, 'source' => '#'.realpath($tempFile) );
//$attachement = array('access_token'=>$fbme['access_token'], 'source' => '#'.realpath($tempFile) );
$fb_photo = $facebook->api('me/photos','POST',$attachement);
$FQLQuery = 'SELECT object_id, pid, src_big, link FROM photo WHERE object_id = '.$fb_photo['id'];
$FQLResult = $facebook->api(array( 'method' => 'fql.query', 'query' => $FQLQuery, 'access_token'=>$fbme['access_token'] ));
$targetPhoto = $FQLResult[0];
echo '<center><h1>Image created.</h1><br/><img src="'.$targetPhoto['src_big'].'"/></center><br/>';
How can I run this only when a user clicks on a button or link?

You can't attach PHP code to a HTML input button, only JavaScript.
If you need to have the code written in PHP then you will either have to link directly to the script, which will display a page with your photo on it (Assuming the code is valid), or more likely what you want is to USE AJAX to include it dynamically in an existing page.
Basically something like this would work in JQuery:
<input type="button" onclick="$.load('/link/to/script.php', $('#where_to_put_the_image'));" />

You could use a form HTML tag. This is just an example:
<form method="post" action="yourphpfile.php">
<input type="text" name="fname" value="your first name?" />
<input type="submit" value="run" />
</form>

Put it in a file and then link to that file. Simple.
Or do you want to run it asynchronously on the page? In which case, you'll need to do it using Ajax.

As with triggering any PHP script or function, send a request to the page that contains it, either by a link, a form post, or an Ajax request.

Related

Pass POST over to two pages?

I have the following pages (code fragments only)
Form.html
<form method="post" action="post.php">
<input type="text" name="text" placeholder="enter your custom text />
<input type="submit">
</form
post.php
....
some code here
....
header('Location: process.php');
process.php
on this page, the "text" input from form.html is needed.
My problem is now, how do i pass the input-post from the first page through process.php without loosing it?
i dont want to use a process.php?var=text_variable because my input can be a large html text, formated by the CKeditor plugin (a word-like text editor) and would result in something like this process.php?var=<html><table><td>customtext</td>......
How can i get this problem solved?
I would like to have a pure php solution and avoid js,jquery if that is possible.
If you don't want to use $_SESSION you can also make a form in the page and then send the data to the next page
<form method="POST" id="toprocess" action="process.php">
<input type="hidden" name="text" value="<?php echo $_POST["text"]; ?>" />
</form>
<script>
document.getElementById("toprocess").submit();
</script>
or you can the submit the form part to whatever results in moving to another page.
Having said that using the $_SESSION is the easiest way to do this.
Either use $_SESSION or include process.php with a predefined var calling the post.
$var = $_POST['postvar'];
include process.php;
Process.php has echo $var; or you can write a function into process.php to which you can pass var.
maybe the doc can help: http://php.net/manual/fr/httprequest.send.php
especially example #2:
$r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
$r->setOptions(array('cookies' => array('lang' => 'de')));
$r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
$r->addPostFile('image', 'profile.jpg', 'image/jpeg');
try {
echo $r->send()->getBody();
} catch (HttpException $ex) {
echo $ex;
}
But I wouldn't use this heavy way where sessions are possible and much easier, see previous answer/comments.
This is ok for instance if you want to call a pre-existing script awaiting post-data, if you can't (or don't want to) modify the called script. Or if there's no possible session (cross-domain call for instance).

Constructing a URL using $_POST in PHP

I am creating a page where users can submit metadata about content to a database by submitting the Digital Object Identifier (DOI) of the content. The site will then go and look up the metadata of content on www.crossref.org and present a summary of the data before adding it to the database
I have created a form for users to enter the DOI
<FORM ACTION="newref-getxml.php" METHOD=POST>
<P><strong>New Reference Form</strong><BR>
DOI: <INPUT NAME="send_doi"><BR>
<BR>
<INPUT TYPE=SUBMIT NAME="submitref" VALUE="SUBMIT">
</FORM>
And a file to fetch and read the XML (I have removed my API KEY from the URL for obvious reasons)
<?php
echo $_POST[send_doi]; // check post data is coming though
$xml = simplexml_load_file("http://www.crossref.org/openurl/id=doi:'$_POST[send_doi]'&noredirect=true&pid=APIKEY&format=unixref");
?>
<p>
Title: <?php echo $xml->doi_record->crossref->journal->journal_article->titles->title;?><br />
Year: <?php echo $xml->doi_record->crossref->journal->journal_article->publication_date->year;?><br />
Journal: <?php echo $xml->doi_record->crossref->journal->journal_metadata->full_title;?><br />
DOI: <?php echo $xml->doi_record->crossref->journal->journal_article->doi_data->doi;?>
</p>
The problem is with inserting the user submitted DOI into the URL, I thought I could just paste '$_POST[send_doi]' into the URL where the DOI should go, but that is not working.
All I get is
10.3998/3336451.0009.101
Title:
Year:
Journal:
DOI:
When submitting a DOI
How you write the URL to include the '$_POST[send_doi]' value?
simplexml_load_file("http://www.crossref.org/openurl/id".
"?doi=".urlencode($_POST[send_doi]).
"&noredirect=true&pid=APIKEY&format=unixref");
Added a question mark as I don't see it in your URL. A better alternative is http_build_query(). Check it out!
simplexml_load_file('http://www.crossref.org/openurl/id?'. // <- Question Mark here
http_build_query(array(
'doi' => $_POST[send_doi],
'noredirect' => 'true',
'pid' => 'APIKEY',
'format' => 'unixref',
))
);
Try this:
$xml = simplexml_load_file("http://www.crossref.org/openurl/id=doi:'{$_POST['send_doi']}'&noredirect=true&pid=APIKEY&format=unixref");
Array keys of type string need to be encapsulated in single quotes. When including a variable into a string, use {} to enclose the variable itself.
Also, make sure you validate that input so you dont have erraneous calls going to that API. Regex works well. See: preg_match().

Log into a site using PHP's HttpRequest?

I'm looking for a way to search for business "X" on LinkedIn, which requires a valid login session. I'm making Http requests with PHP's HttpRequest object, but I'm not familiar enough with handling session data/cookies to figure out how to pinpoint my mistake.
$loginURL = "https://www.linkedin.com/uas/login-submit";
$loginPostData = array("session_key" => "mylogin#mydomain.com",
"session_password" => "itsasecret",
"source_app" => "",
"signin" => "",
"session_redirect" => "",
"csrfToken" => "",
"sourceAlias" => "",
);
$httpRequest = new HttpRequest($loginURL, HttpRequest::METH_POST);
$httpRequest->setContentType("application/x-www-form-urlencoded");
$httpRequest->addPostFields($loginPostData);
$httpRequest->setOptions(array("redirect" => 10));
$httpMessage = $httpRequest->send();
$responseBody = $httpMessage->getBody());
I write $responseBody to a file and open it in a browser, but it only contains a generic LinkedIn page with log in fields. Using the "Tamper Data" extension to firefix, I can see that the following request headers allow firefox to load a session-viewable webpage:
Host=www.linkedin.com
Content-Type=application/x-www-form-urlencoded
Content-Length=124
POSTDATA=source_app=&session_key=mylogin#mydomain.com&session_password=itsasecret&signin=&session_redirect=&csrfToken=&sourceAlias=
There's something weird about the data I get back too: I'd expect to see something like "invalid username or password", but I get the default linkedin.com page.
Actually looking at the code of linkedin there are some hidden input that you need to send as POST datas, because your request has not one or more of these values you got the same login page...
Copy & paste from linkedin
<input type="hidden" name="source_app" value="">
<input type="hidden" name="trk" value="guest_home_login">
<input type="hidden" name="session_redirect" value="" id="session_redirect-login"><input type="hidden" name="csrfToken" value="ajax:9034582961576232024" id="csrfToken-login"><input type="hidden" name="sourceAlias" value="0_7r5yezRXCiA_H0CRD8sf6DhOjTKUNps5xGTqeX8EEoi" id="sourceAlias-login">

How send parameter to a url without using form in php?

dear all,
I need to send parameters to a URL without using form in php and get value from that page.We can easily send parameters using form like this:
<html>
<form action="http://..../abc.php" method="get">
<input name="id" type="text" />
<input name="name" type="text"/>
<input type="submit" value="press" />
</form>
</html>
But i already have value like this
<?php
$id="123";
$name="blahblah";
?>
Now i need to send values to http://..../abc.php without using form.when the 2 value send to abc.php link then it's show a value OK.Now i have to collect the "OK" msg from abc.php and print on my current page.
i need to auto execute the code.when user enter into the page those value automatically send to a the url.So i can't use form or href. because form and href need extra one click.
Is their any kind heart who can help me to solve this issue?
You can pass values via GET using a hyperlink:
<a href='abc.php?id=123&name=blahblah' />
print_r($_GET) would then give you the values, or you can use $_GET['id'] etc in abc.php
Other approaches, depending on your needs, include using AJAX to POST/GET the request asynchronously, or using include/require to pull in abc.php if it only includes specific functioanlity.eg:
$id="123";
$name="blahblah";
require('abc.php');
You can do:
$id="123";
$name="blahblah";
echo "<a href = 'http://foo.com/abc.php?id=$id&name=$name'> link </a>";
<?php
$base = 'http://example.com/abc.php';
$id="123";
$name="blahblah";
$data = array(
'id' => $id,
'name' => $name,
);
$url = $base . '?' . http_build_query($data);
header("Location: $url");
exit;

implement a button to send information to another php file?

I've got the following php code printing out the contents of a SQL table.
$query="select * from TABLE";
$rt=mysql_query($query);
echo mysql_error();
mysql_close();
?>
<i>Name, Message, Type, Lat, Lng, File </i><br/><br/>
<?php
while($nt=mysql_fetch_array($rt)){
if($nt[name] != null){
echo "$nt[id] $nt[name] $nt[message] $nt[type] $nt[lat] $nt[lng] $nt[file]";
}
}
?>
How would I implement a button so for each "row" if the button is clicked on that row it'll submit the information of that row to another php file?
I want it looking something like...
details details2 details3 BUTTON
details4 details5 details6 BUTTON
details7 details8 details9 BUTTON
details10 details11 details12 BUTTON
Where if BUTTON was hit on row 1 details1,2,3 would be sent to a php file, on row 2 detals 4,5,6 would be sent etc. How would I do this?
Thanks.
it's going to be something like that, depending on the data you need to send:
while($nt = mysql_fetch_array($rt)) {
if($nt[name] != null){
echo "$nt[id] $nt[name] $nt[message] $nt[type] $nt[lat] $nt[lng] $nt[file] ".'send request<br/>';
}
}
You can either use GET method and send a query string to the second php page and receive the variables there, like
next.php?variable1=value1&variable2=value2&...
or use POST method by making a hidden form for each row and assign a hidden field for each variable you want to send.
<form method="post" action"next.php">
<input type="hidden" name="variable1" value="value1" />
<input type="hidden" name="variable2" value="value2" />
.
.
.
</form>
or instead of sending all the values, just send the row ID (if any) using any of these two methods and run another query in next.php to get the information you need from database.
Instead of submitting the entire data, just send the ID and fetch the results from the database in the other script. If you want to have an input button, you can do
<form action="/other-script.php" method="GET">
<?php printf('<input type="submit" name="id" value="%s" />', $nt["id"]); ?>
</form>
but you could also just add a link, e.g.
printf('Submit ID', $nt["id"]);
If you really want to send the entire row values over again, you have to make them into form inputs. In that case, I'd send them via POST though.

Categories