What is wrong with my URL and _POST? - php

For some reason I can't get this to work. It pulls the name and team but not the other data.
Here is my _POST data:
$id=$_GET['name'];
$tm=$_GET['team'];
$hr=$_POST['hours'];
$bl = $_POST['block'];
$sp = $_POST['spec_area'];
$wx = $_POST['wx'];
Here is my URL:
update</td>
And here is where I am trying to put it (testing only of course):
<?php
echo $tm;
echo $wx;
echo $hours;
echo $hr;
?>
So when I click the link obviously I want it to post the data... What am I doing wrong?

If you are sending the data via the URL in the way you are, it can only be retrieved with $_GET. The $_POST array is for data submitted in a <form> tag.
http://www.tutorialspoint.com/php/php_get_post.htm

You are passing variables via GET request, but you're trying to retreive some of them via $_POST, just change them all to $_GET:
$id = $_GET['name'];
$tm = $_GET['team'];
$hr = $_GET['hours'];
$bl = $_GET['block'];
$sp = $_GET['spec_area'];
$wx = $_GET['wx'];
If you want to use $_POST then you need to send data through <form> with method="POST".
PHP Manual: Variables From External Sources
You should also be aware, that using it this way leads to XSS vulnerabilities.

Related

Post url in form with get method

I have a problem today!
I am trying to post a URL in form via GET method
When I post URL it automatically converts to http://example.com/?url=http%3A%2F%2Fanonylinq.com%2F%3Fi%3Dphpphp from http://anonylinq.com/?i=phpphpIs there any way to solve this problem? I am doing this via PHP.
because I want to echo "i" as - <?php echo $i; ?> Everything else is done but I am stuck at this point.
Already done this too -
$urlSplitted = explode('?i=', $_GET['url']); $i = $urlSplitted[1];
if you want to go this road:
$urlSplitted = explode('?i=', $_GET['url']); $i = $urlSplitted[1];
you should use
$urlSplitted = explode('%3Fi%3D', $_GET['url']); $i = $urlSplitted[1];
Take a look at http://php.net/manual/en/function.urldecode.php
That should do the trick for it.
e.g.
$string = $_GET['url'];
$decoded = urldecode($string);
$urlSplitted = explode('?i=', $decoded );
$i = $urlSplitted[1];
I have done this:
Just made form with post method to other file having meta refresh and echoed url in meta refresh value! Thats it! Meta refresh will not encode your url.

Get content after question mark in PHP

I am getting a request like this and the url looks like this : www.site.com/test.php?id=4566500
Now am trying to get the id number to make the code in test page work, is there a way to do this?
<?php
echo("$id"+500);
?>
You can access these values via the $_GET array:
<?php
echo($_GET['id'] + 500);
?>
This is basic PHP. You want to use the $_GET superglobal:
echo $_GET['id'] + 500;
Do not forget to check the right setting of your Getter parameter:
if (isset($_GET['id']) && preg_match("\d+", $_GET['id'])) {
// do something with $_GET['id']
} else {
// appropriate error handling
}
Remember that anyone can set the id parameter to any value (which can lead to possible XSS attacks).
You cannot access direct url parameter without using predefined PHP super global variable like $_GET["$parameter"] OR $_REQUEST["$parameter"].
So for : www.site.com/test.php?id=4566500
<?php
$id = (int)$_GET['id']; // Or $_REQUST['id'];
if(is_numeric($id)){
echo $id + 500;
}else{
echo $id;
}
?>
For more detail :
PHP $_GET Reference
PHP $_REQUEST Reference

Link value of form in PHP

I have an html file, basically a simple form: The purpose is to submit a value that runs a piece of code on a PHP file ('alternative.php') See sample of html code:
<form name="input" action="alternative.php" method="POST">
Area: <input type="text" name="area"><br><br>
<input type="submit"><br>
</form><br><br>
This runs smoothly
Now I have a second PHP file ('alternative2.php') and this file automatically needs to link to the data that is input in the form.
Excerpt of php code for alternative2:
<?php
require_once 'header.php';
/** Create HTTP POST */
$accomm = 'ACCOMM';
$region = '';
foreach ($result->area as $entry) {
$region = $entry->attributes()->area_name;
break;
}
$page = '10';
Both alternative.php and alternative2.php require header.php.
Excerpt of header.php:
<?php
/** Create HTTP POST */
$country = 'Australia';
$area = htmlspecialchars($_POST["area"]);
$seek = '<parameters>
<row><param>COUNTRY</param><value>'. $country .'</value></row>
<row><param>AREA</param><value>'. $area .'</value></row>
</parameters>';
Currently it returns "Notice: Undefined index: area in C:\xampp\htdocs...." when I run it.
How do I go about this?
Thanks
The error suggest that $_POST["area"] is not defined, if you don't reach alternative2 from your form, then that's why you see this; if you want to reach alternative2 from other place (for example directly), or if the value of a variable must be the same on several pages, then you may want to consider using Sessions.
Check if the $_POST values exist before using them...
if (isset($_POST["area"])){
//do stuff
}
Basically it has to do with which file your form actually submits to. If your form submits to only alternative.php then you aren't receiving the POST information to your second page. The easiest and logical choices in my opinion (based on what I see from your code) is to merge the functionality of alternative.php and alternative2.php into a single page, or use sessions to store the POST information which will then be available to both pages. If you were to use sessions you would be doing something like shown below.
Start with the file that handles your form input (alternative.php I presume) and add
session_start();
to the top of that file. Then, in whatever block of code you have getting your form information add the following line:
$_SESSION['area'] = $_POST['area'];
Now your information is stored and will be available from request to request.
Then in your head.php file, access the info via $_SESSION variables.
<?php
/** Start Session */
session_start();
/** Create HTTP POST */
$country = 'Australia';
$area = htmlspecialchars($_SESSION['area']); //Access your session variable.
$seek = '<parameters>
<row><param>COUNTRY</param><value>'. $country .'</value></row>
<row><param>AREA</param><value>'. $area .'</value></row>
</parameters>';
?>

Receiving JQuery $.post data and returning specific results with PHP

I'm trying to make a dynamic website on a single webpage and I'm almost there. All that's left is to do some php coding.
Here is a few lines of code from my "index.php"
$('.open').click(function(){
current = $(this).html();
$.post("source.php", {name: current}, function(src){
$('#codeBox').html(src);
});
});
How do I check the value of "current" in my php file and return data specific to the link I click on?
Simply check the POST parameters :
$name = $_POST['name'];
Don't forget to sanitize your inputs.
What's the content of you .open element ? Maybe it would be preferable to check the element's id, compare the html make me surprising.
$val = $_POST["name"];
$a = array();
switch($val) {
case 'some val':
$a['something'] = "something else";
print json_encode($a);
break;
...
}
<?PHP
// $_POST['name'], this will give you the value of name in the php file
echo $_POST['name']; // this will output it
?>
Try this
$current = $_POST["name"];
using $current you can return the data conditionally.

How to keep all the POST information while redirecting in PHP?

header('Location: ' . $uri);
This will miss all the $_POST information.
Don't use $_SESSION as you have been suggested. Session data is shared with all other pages, including the ones open in other tabs. You may get unpredictable behaviour if you use the same trick in multiple places of your website.
An untested better code would be something like this.
session_start();
$data_id = md5( time().microtime().rand(0,100) );
$_SESSION["POSTDATA_$data_id"] = $_POST;
header('Location: ' . $uri."?data_id=$data_id");
In the next page you may retrieve the previous post like this
session_start();
$post = array();
$data_key = 'POSTDATA_'.$_GET['data_id'];
if ( !empty ( $_GET['data_id'] ) && !empty( $_SESSION[$data_key] ))
{
$post = $_SESSION[$data_key];
unset ( $_SESSION[$data_key] );
}
The code above is not tested, you may have to deal with some error before it works.
if u want to carry forward your POST data to another pages ( except the action page) then use
session_start();
$_SESSION['post_data'] = $_POST;
Indeed, you can't redirect POST requests.
Either let your server proxy the request (i.e. make a cURL request to the other site) or create another form, fill it with hidden fields and submit it with Javascript/let the user click.
Alternatively, as #diEcho says, depending on what you're trying to do: sessions.
If you perform a redirect the post will be lost and a GET will occur.
You could save your POST in a SESSION or encode it in the GET (as query string)
You could save the post data in the session, redirect, and then retrieve it back from the session.
session_start();
$_SESSION['POSTDATA'] = $_POST;
header('Location: ' . $uri);
Then in the PHP file for the new location, retrieve the post data like this:
$_POST = $_SESSION['POSTDATA'];
I do use my own simple method.
Just think very logical! For example if you use a text attribute which you want to keep the value of, you can use:
<?php $textvalue = $_POST['textname']; ?>
<input type="text" name="textname" value="<?php echo $textvalue; ?>" />
<br />
//And if you want to use this method for a radio button, you can use:
<?php if(isset($_POST['radio1']){
$selected = "selected";
} ?>
<input type="radio" name="radio1" <?php echo $selected; ?> />

Categories