newbie_n
newbie_n

Reputation: 27

How can I send an array with href attribute in php?

load_displayreport.php

This file contains an array

$fieldsString1="";

while ($reportRow1 = mysqli_fetch_array($reportResult1)) {    
    array_push($fieldNames, $reportRow1['field_name']);
    $fieldsString1.="reg.".$reportRow1['field_name'].",";
 }

I am sending this array with some parameters to another file called printdynamicreport.php

echo "<div class='print'>
<a href ='../print/printdynamicreport.php?hid=$hid&fieldsString1=$fieldsString1&tablename=$tablename&start=$start&stop=$per_page'> Save as pdf </a></div>";

printdynamicreport.php

Accepting the value of 'fieldsString1' in one variable.

$fieldsString1 = $_GET['fieldsString1'];

since I have to use this variable in javascript m doing following :

var fieldsString1 = <?php echo $fieldsString1; ?>;

Now my question, is this possible ? can I send an array and use it like the way I have used ? Is there any alternative ? Thanks in advance :)

Upvotes: 1

Views: 4164

Answers (4)

Peter
Peter

Reputation: 16933

Passing complex data in GET request

To build a link:

$array = Array([contents of big array]);
$url = "page.php?" . http_build_query(Array(
   "array" => $array
));

To read data from $_GET in page.php:

$array = $_GET['array'];

That's it ;)

Docs:


In your example:

load_displayreport.php

$query = http_build_query(Array(
    'fieldNames' => $fieldNames,
    'hid' => $hid,
    'tablename' => $tablename,
    'start' => $start
    'stop' => $per_page
));
echo "<div class='print'>
<a href ='../print/printdynamicreport.php?{$query}'> Save as pdf </a></div>"

printdynamicreport.php

$fieldNames1 = $_GET['fieldNames1'];

and

var fieldNames = <?php json_encode($fieldNames1); ?>

Upvotes: 2

Ed Ganiukov
Ed Ganiukov

Reputation: 91

You can create some string from this

array(implode('|', $fieldsString1)),

send this string , and on other side create array from

string (explode('|', $fieldsString1))

this will work like callback

Upvotes: 0

u_mulder
u_mulder

Reputation: 54841

Use http_build_query function.

Upvotes: 0

Abhishek
Abhishek

Reputation: 567

yes you can here is a example to it

If you want to do this with an associative array, try this

$url = 'http://example.com/index.php?';
$url .= implode('&amp;', array_map(function($key, $val) {
    return 'aValues[' . urlencode($key) . ']=' . urlencode($val);
  },
  array_keys($aValues), $aValues)
);

callback

function urlify($key, $val) {
  return 'aValues[' . urlencode($key) . ']=' . urlencode($val);
}

$url = 'http://example.com/index.php?';
$url .= implode('&amp;', array_map('urlify', array_keys($aValues), $aValues));

Upvotes: 1

Related Questions