how to display the submitted data using $_POST - php

I have an input named like this:
name="ListPassengers[0].Lastname"
How can I display the value, because when I use $_POST['ListPassengers[0].Lastname']
I get the following error:
Notice: Undefined index: ListPassengers[0].Name in C:\wamp\www\3\res.php

Use print_r($_POST). You can also use print_r() for printing $_GET values as well as arrays etc.

all the values passed from one page is stored in $_post variable and these are indexed with names which u have sent them so u just have to use those i.e input field name etc to refer its data
one way to post all data is same as given above of print_r().
u can also use var_dump($_POST)
or u can access one by one using a for loop also
` foreach ($_POST as $key => $value)
echo $key.'='.$value.'<br />'; `

Related

getting the count value in PHP using value command

I am making a dashboard using php and displaying it on webpage using html. For this I am taking some count values. Is !$value[" variable"] valid in php? How do i get the not of (!=) value? There is no other way to get the data. I am getting the values from a database.
print "<td><a href=\"$strBase and status!='Closed'\" target=\"_blank\">".$value[" ? "]."</td>";
I need to print !closed. Somehow I do not know how to pass that inside $value[].
If you want to use a variable as a key in order to retrieve a value from an array you can do as follows (I use a snippet of your code):
.$value[$myVar].
Where $myVar should be a string with the name of the key you want to retreive from $value
But anyhow, your question is pretty unclear so I'm not sure if this is what you are looking for.
You can check if the variable is empty(), i.e.:
if(empty($value["variable"])){
echo "variable is empty";
}

json decode certain duplicated value with php

Here's the link i want to decode:
https://api.instagram.com/v1/users/28855276/media/recent/?client_id=775829274b6f48c1ac2bf218dda60e16
Actually i've tried many methods to get the result i want but i failed to do so with PHP.
i need to extract a value from this json which should equal a certain variable
Example:
$variable = json_value
The Hard thing here that i want a duplicated value.
the value i need is:
profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_28855276_75sq_1348344197.jpg"
Do not know how to reach this value.
Here's it's place in the json file
{"username":"zedd","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_28855276_75sq_1348344197.jpg","id":"28855276","full_name":"Zedd"}
Please note that i've the username and id values already known.
Need to extract the profile_picture link to a PHP variable.
Please note: profile_picture is duplicated.
There is no duplication by me.
<?php
$array = json_decode('{"username":"zedd","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_28855276_75sq_1348344197.jpg","id":"28855276","full_name":"Zedd"}',true);
echo $array['profile_picture'];
?>

How to get values from url in php

I am tring to send values from one page to others from following url in php
result.php?1=C&2=C&3=C
But i am not able to access values on second page.
you can get value from following url result.php?value=1&val=2 using
$_GET['value'];
$_GET['val'];
Because you cannot assign a variable to start with integer
To receive values across pages we usually use $_GET['key']
to receive the values you sent
just use
echo $_GET['1'];
so everything before the = sign(LHS) ie is 1,2,3 are the keys in your case and the value would be stored in $_GET['1'], $_GET['2'], $_GET['3']
PHP can access query string parameters with the superglobal array $_GET.
For example;
If URL is http://example.com/script.php?var1=hello
echo $_GET['var1']; //hello is echoed.

Using a php variable inside an sql query

In my php script,i am using a php variable inside an sql query.The php variable acquires its value through a post variable as follows:
$desc0=$_POST['desc0'];
$desc1=$_POST['desc1'];
$desc2=$_POST['desc2'];
$desc3=$_POST['desc3'];
$desc4=$_POST['desc4'];
$desc5=$_POST['desc5'];
$desc6=$_POST['desc6'];
$desc7=$_POST['desc7'];
$desc8=$_POST['desc8'];
$desc9=$_POST['desc9'];
The query is:
for($i=0;$i<10;$i++)
{
$q="insert into photos(name,category,description) values ('{$name{$i}}','$category','{$desc{$i}}')";
}
The problem is that on submitting the form i am getting an error which says
"undefined variable desc".
Therefore its not taking the values from the previously defined variables?
Any help?
First of, you code is completely unsafe - you should not pass user data directly into your query. There are many topics about it, and this is a good start.
Next, you don't need to store your data in such weird way. What if you'll want to pass 20 photos? In HTML, name your fields like photos[] - and in PHP, your values will be correctly parsed as an array $_POST['photos'], so you will be able to work with array:
$photos = $_POST['photos'];
foreach($photos as $photo)
{
//$photo contains certain item, so handle it with your logic
}
Finally, your issue is because of non-obvious PHP possibility for array-dereference with curly brackets. So your $desc{$i} is an attempt to access $i-th index for non-existent array $desc. Either use $desc$i or use concatenation to separate your variables.
You must change $desc{$i} to ${"desc" . $i}

What's the correct way to access a field in an array element with php?

I'm getting a few rows from the DB with an ajax call in PHP and I save the result to an array with javascript.
Then I make some changes in the data and wish to update the DB.
So I use another ajax call for that but I can't manage to access the fields inside the rows correctly.
When I try this I get nothing:
echo $bArray[$i].branchId;
When I try this:
echo json_encode($bArray[$i].branchId);
I get ArraybranchId instead of the field value.
What's the correct way to access the field with php?
Try either for array:
$bArray[$i]['branchId']
or for object:
$bArray[$i]->branchId
depending which type $bArray[$i] is (array or object). You have not written in your question, so I showed both ways.
I take it branchId is the name of the field, and you want the value for that field?
If so, it's:
echo $bArray['branchId']; or
echo $bArray[$i]['branchId']
Edit: Also, you'll need to make sure you're using mysql_fetch_assoc not mysql_fetch_array!

Categories