1

All, something similar seems to have been posted before, however, slim and twig are still very new to me so apologies.

A little back ground, I have an application which posts data to

http://dev.website.com/post.php
Data1="My Date"
Data2="More data"

Eventually it will go in the database, but baby steps, first i just want to know then when i run the post from something like Advanced REST client, it actually shows its been posted.

In old school PHP I can just do this:

<?php
print ("Data1: " . $_POST["Data1"]);
print ("Data2: " . $_POST["Data2"]);
?>

Im trying to do this in slim and twig but I either don't understand it correctly or its not working, could someone tell me what im doing wrong please?

Im using userfrosting for a start which seems to work, i've done the tutorials on the site but they dont really help with this.

in the site index, my routes in index.php look like this:

$app->get('/post.php', function () use ($app) {
    $app->render('post.twig');
});

$app->post('/post.php', function () use ($app) {
    $backup_post_data1= $app->request->post('data1');


    $app->render('post.twig', [ 
      'backup_post_data1' => $backup_post_data1,
  ]);
});

My twig template post.php (That's what it was called in the old PHP site and is hard coded in the application which makes the post)

{% extends "layouts/layout-simple.twig" %}

{% block page %}
{% set page = page | merge({
"title"       : "post page",
"description" : "Accept new data from post."
}) %}
{{ parent() }}
{% endblock %}

{% block content %}
<h1>My data</h1>
<table border="1">
    <tbody>
    <tr>
        <td><strong> Variable</strong></td>
        <td><strong> POST DATA </strong></td>
    </tr>
        <tr>
            <td>
                Data1
            </td>
            <td>
                {{ backup_post_data1 }}
            </td>
        </tr>
    </tbody>
</table>
{% endblock %}

Thanks in advance

Nerden
  • 41
  • 1
  • 6
  • you are likely not setting up the proper content-type header in your rest client [or it could be missing entirely]... Please check that the content-type header is set, and that it is something that Slim parses. ... https://github.com/slimphp/Slim/blob/2.x/Slim/Http/Request.php#L59 – geggleto Aug 04 '16 at 15:45
  • So if i direct my post request to my old PHP page it works fine, if i do it to my new slim/twig one it doesn't. got the type set to application/x-www-form-urlencoded I'm pretty sure i have the slim code wrong somewhere – Nerden Aug 04 '16 at 16:09
  • have you tried to debug the slim app? – geggleto Aug 04 '16 at 17:50
  • Can you share your form with us? And, how are you handling form submission? Are you seeing any 4xx status codes in your [browser console](http://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers) when you submit the form? You can also use your browser console to see the raw contents of your POST request when you submit the form. – alexw Aug 04 '16 at 21:09
  • Hi Alex,it's an application which makes the post not a form. the old PHP page is happy recieving the data with $_POST['field'] syntax – Nerden Aug 04 '16 at 21:15
  • 1
    On top of the question you say you post your parameter as `Data1` but then you read the `data1` one. Can you use lowercase in the two cases and see if this is the issue? – Davide Pastore Aug 07 '16 at 14:58
  • You still haven't answered my question - are you getting an error code in the response? All you've said is "its not working", which doesn't help at all. – alexw Aug 12 '16 at 18:22
  • Hi Alex, i am not seeing any error codes, I think its my understanding of how slim works thats at fault, i've found some tutorials which i think work similarly. Thanks for your help – Nerden Aug 13 '16 at 21:38

0 Answers0