I'm populating select box with options from json file and trying to set value from $_POST as selected. I get all the values printed as options but none selected.
Seems like something goes wrong when comparing $marke to $post so nothing get selected.
<select name="marke" id="marke"class="form-control">
<?php
$url = 'includes/lists/models.json';
$jsonData = file_get_contents($url);
$jsonDataObject = json_decode($jsonData, true);
$post = $_POST['marke'];
$i = 0;
echo '<option>--</option>';
foreach ($jsonDataObject['markes'] as $marke) {
if ($marke==$post) {
echo '<option selected value="'.$marke['title'].'" id="'.$i.'">'.$marke['title'].'</option>';
}else{
echo '<option value="'.$marke['title'].'" id="'.$i.'">'.$marke['title'].'</option>';
}
$i++;
}
?>
</select>
P.S $_POST['marke'] is set correcly.
As I understand it $marke is an array, and most likely $_POST['marke'] is a string or int.
if ($marke['title']==$post) {
instead of
// You'd be comparing a $marke array with $_POST['marke'] string
if ($marke==$post) {
Related
I am having 2 files, one to fetch ajax and to decode it. the other (page2.php) is modal popup in which I want to populate the received data from page1.php.
page1.php is below which returns invoice numbers perfectly.
$newArr = array();
$decoded = array();
$decoded = json_decode($result, true);
foreach($decoded ['invoices'] as $result) {
$newArr = $result['invoiceno'];
echo $newArr; //JUST FOR DEBUGGING
}
page2.php is a modal where I try to get the received data and populate into a dropdown.
function sort_(){
global $newArr;
$output='';
$output.= '<option value = '.$newArr.'>'.$newArr.'</option>';
return $output;
}
I know I am not looping the array to sort. I have tried different ways like below which didn't work.
function sort_(){
global $newArr;
global $result;
global $decoded;
$decoded= json_decode($result, true);
$output='';
$output.= '<option value = "Select INO">Select Select INO</option>';
foreach($decoded['invoices'] as $result) {
$output.= '<option value = "'.$newArr.'">"'.$newArr.'"</option>';
}
return $output;
}
The error that I am getting in developer console is;
<b>Warning</b>: Trying to access array offset on value of type null in <b>C:\xampp\htdocs\order\page2.php</b> on line <b>17</b><br />
<br />
<b>Warning</b>: foreach() argument must be of type array|object, null given in <b>C:\xampp\htdocs\order\page2.php</b> on line <b>17</b><br />
where I am stuck?
Get the records from the DB (In case your data is not in form of a json string in the DB you don't need to decode)
Create the following functions :
function getOptions($acc, $record){
return $acc."<option value='".$record['your key']."'>".$record['your key']."</option>" ;
}
function createOptionsHtml($records) {
return array_reduce($records, 'getOptions') ;
}
Hope I got your problem right.
it was an easy fix.
page1.php had
global $decoded;
$decoded= array();
$decoded= json_decode($result, true);
page2.php
function sort_(){
global $decoded;
global $result;
$_invoiceReturn='';
$_invoiceReturn .= '<option value = "Select Invoice">Select Invoice</option>';
foreach($decoded['invoices'] as $result) {
$invoice_number = $result['invoice_number'];
$_invoiceReturn .= '<option value = "'.$invoice_number.'">'.$invoice_number.'</option>';
}
return $_invoiceReturn;
}
I am looping the invoices on page2.php based retrieving page1.php JSON. I decoded the json so it becomes a normal PHP array and looping through.
Hello I am submitting a form with POST method and I want its contents to be echo'ed one by one apart from the last one. So far I am using
<?php foreach($_POST as $data){
echo $data;
}
?>
which displays the whole array of $_POST, how can I make it using common "for" loop to not echo the last item of the array? It doesnt seem to work
<?php
$length=count($_POST)-1;
for($i=0; $i<$length; $i++) {
echo $_POST[$i];
?>
<br>
<?php } ?>
I am getting 5 errors, undefined offset 0 through 4 where the echo line is present
Do the following:
<?php
$counter = 0;
$lastItemOrder = count($_POST);
foreach($_POST as $value) {
$counter++;
if( $counter !== $lastItemOrder) {
echo $value;
}?>
<br><?php
} ?>
Your loop for just get numerical index like $_POSR[0], $_POST[1]... This just would work if in the HTML the attribute name of the input elements be numerical also, like name="0" and so on.
foreach perform loop on array independently of index, numerical or string.
Try this:
<?php
$counter = 0;
$lastItemOrder = count($_POST);
foreach($_POST as $index => $value) {
$counter++;
if( $counter !== $lastItemOrder) {
echo $index . ": " . $value;
}?>
<br><?php
} ?>
ok now I get it, I didnt know the difference between associative and numeric arrays. I fixed it with an if statement
I want to know how to POST json object fields based
<?php
$jsondata = file_get_contents('https://newsapi.org/v1/articles?source=google-news&sortBy=top&apiKey=xxxxxxx');
$data = json_decode($jsondata, true);
$tasks = $data['articles'];
foreach ( $tasks as $task) {
$news_title = $task['title'];
$news_url = $task['url'];
$news_imagUrl = $task['urlToImage'];
echo "<option value='".$task."'>".$news_title."</option>"; }
?>
Now, i want to post all the values like title, url, urlToImage on sumbit.
How to achieve this?
I'm loading event listing as an JSON-File from an URL:
$file = file_get_contents('http://ecample.com/listing.php?');
$data = json_decode($file);
?>
The direct output looks like this (more values and mor lines):
[{"id":"1","name":"NAME_1","booking_url":"https://ecample.com/Event_ID65654","category_id":"195"},
{"id":"2","name":"NAME_2","booking_url":"https://ecample.com/Event_ID65654","category_id":"195"},
"id":"1","name":"NAME_1","booking_url":"https://ecample.com/Event_ID65654","category_id":"195"}]
I need to search for all entrys with the value "name":"NAME_1" and print out the value of "booking_url".
I tried different things like array_seach() etc. but did not work out.
Any help is appreciated!
try this
<?php
$file = file_get_contents('http://ecample.com/listing.php?');
$data = json_decode($file, true);
foreach ($data as $r)
{
if ($r['name'] == 'NAME_1')
{
echo $r['booking_url'];
}
}
?>
The other option is to access your data in object context, thus:
<?php
$file = file_get_contents('http://ecample.com/listing.php?');
$data = json_decode($file);
foreach ($data as $r)
{
if ($r->name == 'NAME_1')
{
echo $r->booking_url;
}
}
?>
Either one should work just as well.
I am using a URL to fetch data stored/shown within URL. I get all the value of variable using $_REQUEST['v_name'] but if there is a array in URL how can i retrieve that value.
For Example:
WWW.example.com/rooms?&hid=213421&type=E
I got the value hid and type using
$hid=$_REQUEST['hid'];
but in URL like:
WWW.example.com/rooms?&rooms=2&rooms[0].adults=2&rooms[0].children=0&rooms[1].adults=2&rooms[1].children=0
how can i retrieve value of adults and children in each room.
please help.
Thanks in Advance
You could also try something like this, since most of your original $_REQUEST isn't really an array (because of the .s in between each key/value pair):
<?php
$original_string = rawurldecode($_SERVER["QUERY_STRING"]);
$original_string_split = preg_split('/&/', $original_string);
$rooms = array();
foreach ($original_string_split as $split_one) {
$splits_two[] = preg_split('/\./', $split_one);
}
foreach ($splits_two as $split_two) {
if (isset($split_two[0]) && isset($split_two[1])) {
$split_three = preg_split('/=/', $split_two[1]);
if (isset($split_three[0]) && isset($split_three[1])) {
$rooms[$split_two[0]][$split_three[0]] = $split_three[1];
}
}
}
// Print the output if you want:
print '<pre>' . print_r($rooms, 1) . '</pre>';
$valuse = $_GET;
foreach ($valuse as $key=>$value)
{
echo $key .'='. $value. '<br/>';
}