2

I'm trying to pass some data (JSON) to another page by scanning a QR code. The page where the data is send to, contains a HTML form. I want to use that form as a last chance to correct the data before sending it to the database.

I found here at S.O. a way to pass the data using cURL: (https://stackoverflow.com/a/15643608/2131419)

QR code library: http://phpqrcode.sourceforge.net

I use the QR code execute this function:

function passData () {
    $url = 'check.php';
    $data = array('name' => 'John', 'surname' => 'Doe');
    $ch = curl_init( $url );

    # Setup request to send json via POST.
    $payload = json_encode($data);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

    # Return response instead of printing.
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

    # Send request.
    $result = curl_exec($ch);
    curl_exec($ch);
    curl_close($ch);

    # Print response.
    return $result;
}

Create QR code:

QRcode::png(passData(), $tempDir.'007_4.png', QR_ECLEVEL_L, 4);
echo '<img src="'.$tempDir.'007_4.png" />';

Check.php

<?php $data = json_decode(file_get_contents("php://input"), true); ?>

<form method="post" action="handle.php">
    <input type="text" name="name" value="<?php echo $data['name'];?>" /><br />
    <input type="text" name="surname" value="<?php echo $data['surname'];?>" /><br />
    <input type="submit" />
</form>

Problem:

I can pass the data to check.php, but it's returning plain text instead of a useable HTML form.

enter image description here

Hope someone can help!

EDIT

Some clarification:

What I actually want is, to scan the QR code, which executes the passData() function. Then the 'QR code scanner app', needs to open a browser, which shows check.php with the form AND the passed data as the values of the input fields.

Now, I get only the response of check.php (plain text). When I pass an URL instead of the passData() function like:
QRcode::png("http://www.google.com", $tempDir.'007_4.png', QR_ECLEVEL_L, 4);

The app asks if I want to go to http://www.google.com.

Community
  • 1
  • 1
JK87
  • 379
  • 2
  • 12
  • 26
  • Well you need something to render the html, a browser or a webview – Musa Nov 12 '15 at 13:52
  • Thats true. But now, I execute this cURL function (`passData()`) by scanning the QR code, but when I do `QRcode::png('http://www.google.com', $tempDir.'007_4.png', QR_ECLEVEL_L, 4);` it shows me the google website. What I want, is to pass the data to `check.php` and then show `check.php` with the passed variables as the `input` values. – JK87 Nov 12 '15 at 14:03

2 Answers2

5

QR codes cannot execute code. The only executable type of data you can put in a QR code is a URL. That is why using google.com as a URL opens a web browser to that URL. The QR code itself does not render anything.

What your code is doing is fetching the check.php page when the QR code is generated and then storing the output as the raw data. It isn't a webpage, it is a string like you are seeing in your question. You may be able to pass a javascript URL similar to a bookmarklet but its execution would depend on the QR code reader being used.

bookmarklet example

<?php

function passData() {
    // javascript code in a heredoc, you may need to url encode it
    return <<<JS
        javascript:(function() {
          //Statements returning a non-undefined type, e.g. assignments
        })();
JS;
}

A better way to do it would be to have your QR code generate a URL like: http://your-site.com/check.php?name=John&surname=Doe and host check.php on your machine. You can use the $_GET data to populate your form and then use javascript to automatically post it as Jah mentioned.

Community
  • 1
  • 1
Josh J
  • 6,813
  • 3
  • 25
  • 47
  • Thank you for your explanation! I know it can be done by using GET, but are there any POST alternatives? – JK87 Nov 12 '15 at 15:38
  • 2
    @JK87 No. You can try using a javascript url bookmarklet to make a POST request. You cannot make a local HTML form or do a direct POST with a QR code. – Josh J Nov 12 '15 at 16:03
0

Not the best way but you can do something like this.

Check.php:

 <?php
$data = '<form method="post" action="handle.php">
    <input type="text" name="name" value="name" /><br />
    <input type="text" name="surname" value="surname" /><br />
    <input type="submit" />
</form>';

$html = str_replace(PHP_EOL, ' ', $data);
$html = preg_replace('/[\r\n]+/', "\n", $html);
$html = preg_replace('/[ \t]+/', ' ', $html);
$html = str_replace('> <', '><', $html);
?>
<div id="placeholder">
    Write HTML here
</div>

<script type="text/javascript">
    function write_html(id,data){
        var formHtml = data;
        document.getElementById(id).innerHTML = formHtml;
    }
</script>
Jah
  • 986
  • 5
  • 26