PHP post json result to custom url - php

By default on my web service I must make a post request to custom user's url. I can post the array but I would like to post request as a json payload, the code below could be the post data, but I cannot get the data from PHP.
function redirect_to_customer($result = [], $redirect_url = "")
{
$html = '<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body onload="closethisasap();">
<form name="redirectpost" method="POST" action="' . $redirect_url . '">
';
if (!is_null($result)) {
$result = json_encode($result);
$html .= '<input type="hidden" value="' . str_replace('"', "'", $result) . '"> ';
}
$html .= "
</form>
</body>
<script type=\"text/javascript\">
function closethisasap() {
document.forms[\"redirectpost\"].submit();
}
</script>
</html>";
echo $html;
}
result of this code is :
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
</head>
<body onload = "closethisasap();">
<form name = "redirectpost" method = "POST" action = "http://www.sample.com/response.php">
<input type = "hidden" value = "{'code':'-103','message':'order_id must be unique'}">
</form>
</body>
<script type = "text/javascript">
function closethisasap()
{
document . forms["redirectpost"] . submit();
}
</script>
</html>
response.php user's file content :
<?php
print_r($_POST);
?>
after post data i get empty array

All inputs including <input type = "hidden" need a name, so name = "something".

Related

Is there any way in PHP to store $variable of $variable in session

I created a web-page,where you can search plant's scientific name. Type plant name in search_text it will give you results in search_result(live search like google and facebook search bar) . Ex: when you will type C in search input, in search result you will get C related search. Like C typed in search input, in search result it will start showing Cucumber(Cucumis sativus), Capsicum(Capsicum annuum), etc.
Now I want when you will click on Cucumber(Cucumis sativus) in search result, it have to direct to home.php/Cucumber . Or when user click on Capsicum(Capsicum annuum), it have to direct on home.php/Capsicum .
And on home.php in body tag I want to display plant name with their scientific name. And in para tag information related to plant search result.
index.php
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
<style type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </style>
<style type="text/javascript">
function searchq() {
var searchTxt = $("input[name='search']").val();
$.get("search.php", {searchVal: searchTxt}, function(output) {
$("#output").html(output);
});
}
</script>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="search" id="myInput1" autocomplete="off"
placeholder="Search username..." onkeydown="searchq(); " />
<input type="submit" value=">>"/>
</form>
<br>
<div id="output"></div>
</body>
</html>
search.php
<?php
$con = mysqli_connect('localhost', 'root');
mysqli_select_db($con, 'plant');
$output = '';
if (isset($_GET['searchVal'])) {
$searchq = $_GET['searchVal'];
$sql = "select * from type where plant like '%$searchq%'";
$query = mysqli_query($con, $sql) or die("could not search");
$count = mysqli_num_rows($query);
if ($count == 0) {
$output = 'There is no serach result';
} else {
while ($row = mysqli_fetch_array($query)) {
$plantname = $row['plant'];
$sciencename = $row['species'];
$id = $row['id'];
$output .= '<div>' . $plantname . ' ' . $sciencename . '</div>';
}
}
}
echo '<a herf="home.php/">' . $output . '</a></div>';
?>
There are many ways of doing this
Passing the name of the plant as a GET param is not an option?
You could do
echo "<a href='home.php?plantname={$plantname}' target='_blank'>{$output}</a></div>";
As the response of your server, that would create the link and in home.php you retrieve the plant name with $_GET['plantname'].
in home.php you do
if(isset($_GET['plantname'])){
echo $_GET['plantname'];
}
Please correct this line in index.php
<style type="text/javascript">
with
<script type="text/javascript">

Create a team/organizations via trello api

I want to create trello teams automatically via an html form from my website.
I wrote a php script that seems to work. For example I can get list of boards or create a new board. But it does not works to create teams.
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form action="api_method.php" method="post">
Project Name:<br>
<input type="text" name="projectName" value="board_test">
<br><br>
<input type="submit" value="Submit">
</form>
<p>If you click the "Submit" button, the form-data will be sent to a page called "api_method.php".</p>
</body>
</html>
I then use a php script to create the new board:
<?php
require("trello_api.php");
$key = 'mykey';
$token = 'mytoken';
$trello = new trello_api($key, $token);
$data = $trello->request('GET', ('member/me/boards'));
$obj = array('name' => $_POST['projectName']);
$trello->request('POST', ('/boards'),$obj);
echo "Board name: " . $data[0]->name . "\n \n";
echo "New board: " . $_POST['projectName'];
?>
So that works perfectly but not when I try to do the same thing with "organizations" it doesn't work
$trello->request('POST', ('/organizations'),$obj);
Can you please help me.
I found the solution, I had to use the option "displayName" instead of "name"
<?php
require("trello_api.php");
$key = 'myKey';
$token = 'myToken';
$trello = new trello_api($key, $token);
$data = $trello->request('GET', ('member/me/boards'));
$obj = array('displayName' => $_POST['projectName']);
$trello->request('POST', ('/organizations'),$obj);
echo "Board name: " . $data[0]->name . "\n \n";
echo "New board: " . $_POST['projectName'];
?>

PHP post data to user custom url without as an array

simply i can use this below code to post any result as an array with message as json format for example:
array('result'=>"{'code':'101', 'message':'ok!!'}")
this format result for my users is not standard and they like to get the result only as an json format for example:
{'code':'101', 'message':'ok!!'}
I can't find other code to instead of my code for return the result
My code:
function redirect_to_customer($result = [], $redirect_url = "")
{
$html = '<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body onload="closethisasap();">
<form name="redirectpost" method="POST" action="' . $redirect_url . '">
';
if (!is_null($result)) {
$result = json_encode($result,true);
$html .= '<input type="hidden" name="result" value="' . str_replace('"', "'", $result) . '"> ';
}
$html .= "
</form>
</body>
<script type=\"text/javascript\">
function closethisasap() {
document.forms[\"redirectpost\"].submit();
}
</script>
</html>";
echo $html;
}
You can post an array in a html form, by using brackets in the field's name:
<form action="" method="post">
<input type="hidden" name="result[]" value="'code':'101'" />
<input type="hidden" name="result[]" value="'message':'ok!!'" />
<input type="submit" value="submit" />
</form>
Will output:
array(1)
["result"]=>
array(2) {
[0]=>
string(12) "'code':'101'"
[1]=>
string(16) "'message':'ok!!'"
}
}

How can I change an array value with a click?

I've seen references to Ajax for this, but I'm not entirely sure how I could integrate that with the arrays and still have a functioning system. I want to change the value of $place and $title each time a user presses the button, and I know that I'll need an if statement and some way of processing a form (no clicks in PHP), but I don't know anything else beyond that. I've pasted my PHP and the HTML button below:
<!DOCTYPE html>
<html>
<head>
<title>StatsCalc V1</title>
<link rel="stylesheet" type="text/css" href="sheen.css">
<link rel="script" type="text/javascript" href="slide.js">
<link rel="icon" type="image/png" href="favicon.ico">
<link href='http://fonts.googleapis.com/css?family=Raleway:500' rel='stylesheet' type='text/css'>
</head>
<body>
<?php
$place = 0;
$title = 0;
echo '<div class="boxes"><h1>Statistics Calculator: Version 1</h1></div>';
$links = array('<div class="fancyBoxes"><img class="mainPic" src="normalDist.svg" alt="a normal distribution, courtesy openclipart.org"></div>', "", "");
echo $links[$place];
/*if($_GET){
if(isset($_GET['clickies'])){
$place = 1;
$title = 1;
}
}*/
if($_POST['clickies'] and $_SERVER['REQUEST_METHOD'] == "POST"){
$place = 1;
$title = 1;
}
$subtitle = array('<div class="boxes"><h1>Standardised Score</h1></div>', '<div class="boxes"><h1>Standard Deviation</h1></div>', '<div class="boxes"><h1>Averages</h1></div>');
echo $subtitle[$title];
?>
<input type="submit" id="clickies" name="clickies" value="" />
<script src="move.js"></script>
<script src="slide.js"></script>
<script src="jquery-1.11.2.js"></script>
<!--The calculator must be able to field standardised score, standard deviation, and averages (mean/median/mode)-->
<!--Headings will be assigned with an array; slides will be navigated with tabs on either side of the window-->
<!--Credit to https://openclipart.org/detail/171055/normal-distn-shaded-outside-1s-by-oderwald-171055 for the Standardised Score image/label thingy-->
</body>
</html>
maybe this could give you idea on how to do it.
if($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($_POST['clickies'])) {
$count = intval($_POST['count']);
$count++;
$place = $count;
$title = $count;
}
} else {
$place = 0;
$title = 0;
}
echo '<div class="boxes"><h1>Statistics Calculator: Version 1</h1></div>';
$links = array('<div class="fancyBoxes"><img class="mainPic" src="normalDist.svg" alt="a normal distribution, courtesy openclipart.org"></div>', "", "");
echo $links[$place];
echo $place; echo "<br/>";
$subtitle = array('<div class="boxes"><h1>Standardised Score</h1></div>', '<div class="boxes"><h1>Standard Deviation</h1></div>', '<div class="boxes"><h1>Averages</h1></div>');
echo $subtitle[$title];
?>
<form action="" method="POST">
<input type="submit" id="clickies" name="clickies" value="" />
<input type="hidden" id="count" name="count" value="<?php echo $place;?>" />
</form>
what I did is need to put the button inside a form and put a method= post
and make it submit it to the same page
i also store the count in a hidden input and pass it always when the form is submitted and update its value on the form,
try to modify according to your need
<?php
$_SESSION["place"] = 0;
$place = 0;
$title = 0;
session_start();
echo '<div class="boxes"><h1>Statistics Calculator: Version 1</h1></div>';
$links = array('<div class="fancyBoxes"><img class="mainPic" src="normalDist.svg" alt="a normal distribution, courtesy openclipart.org"> </div>', "", "");
if($_POST['clickies'] and $_SERVER['REQUEST_METHOD'] == "POST"){
$_SESSION["place"] = $_SESSION["place"] + 1;
$title = 1;
}
echo $links[$_SESSION["place"]];
$subtitle = array('<div class="boxes"><h1>Standardised Score</h1></div>', '<div class="boxes"><h1>Standard Deviation</h1></div>', '<div class="boxes"><h1>Averages</h1></div>');
echo $subtitle[$title];
?>
<form method="post" action="">
<input type="submit" id="clickies" name="clickies" value="submit" />
</form>
you can use session of php.

flickr.photos.search

I would like to ask for help. I got this homework to create a class in PHP with search method which returns 50 pictures from Flickr, but without use of official PHP Flickr libraries... Anyway with help of google I wrote following code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
class Flickr {
private $apiKey = 'aba429532a6606f2ee35e3f47a300716';
public function search($query = null) {
$search = 'http://flickr.com/services/rest/?method=flickr.photos.search&api_key=' . $this->apiKey . '&text=' . urlencode($query) . '&per_page=50&format=php_serial';
$result = file_get_contents($search);
$result = unserialize($result);
return $result;
}
}
?>
<form action="index.php" method="get">
<input type="text" name="text1" /><br />
<input type="submit" name="submit1" value="Send" /><br />
</form>
<?php
if(isset($_GET["submit1"])) {
$Flickr = new Flickr;
$data = $Flickr->search($_GET["text1"]);
foreach($data['photos']['photo'] as $photo) {
echo '<img src="http://farm' . $photo["farm"] . '.static.flickr.com/' . $photo["server"] . '/' . $photo["id"] . '_' . $photo["secret"] . '.jpg">';
}
}
?>
</body>
</html>
That code does what I need, but I am not sure whether I can use flickr.photos.search, so I was wondering if there is a way to avoid using flickr.photos.search.
You could just request http://www.flickr.com/search/?q={work_by_witch_you_search}&z=m pictures from here with curl and just parse received page. Then you definitely wount use anything even close to Flickr API.

Categories