Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

HTML Form in TPL
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
edidur
Smarty Rookie


Joined: 21 May 2020
Posts: 12

PostPosted: Thu May 21, 2020 11:01 pm    Post subject: HTML Form in TPL Reply with quote

I am trying to include a simple HTML form in a tpl file, it is working but I am unable to get the form data to be saved into the action URL.

Here is what I have:

Code:

<form action="https://www.edidur.com/api.php?call=writingstats&user_id={$user->_data['user_id']}&word_count={$smarty.post.wcount}&writing_date={$smarty.now|date_format:"%Y-%m-%d"}" method="post">
  <label for="wcount">Update Word Count:</label><br>
  <input type="text" id="wcount" name="wcount">
  <input type="submit" value="Submit">
</form>


user_id and writing_date are both being picked up in the url, but wcount is always returning 0 (zero).


Edited to add:

If I change the wcount variable with a hardcoded number (like 1234) it works perfectly.

Code:

https://www.edidur.com/api.php?call=writingstats&user_id={$user->_data['user_id']}&word_count=1234&writing_date={$smarty.now|date_format:"%Y-%m-%d"}" method="post"


Anything obvious that I am doing wrong?

Have spent about 9 hours on this so far any help would be greatly appreciated.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Fri May 22, 2020 1:29 am    Post subject: Reply with quote

The variable $smarty.post will be populated with the values in the prior POSTing of the form. Therefore, for the first appearance of the form in the browser, there will be no value for $smarty.post.wcount.

So, you say you are POSTing the form, probably with a value in wcount.

Maybe some part of your PHP code is inadvertently unsetting $_POST['wcount'] before Smarty is called to render the template. You might want to check on the value of $_POST before calling Smarty->display(). (I don't know if Smarty grabs the request variables at time of its instantiation, or at some point when rendering the template.)
Back to top
View user's profile Send private message
edidur
Smarty Rookie


Joined: 21 May 2020
Posts: 12

PostPosted: Fri May 22, 2020 1:41 am    Post subject: Reply with quote

Definitely worth a shot, I will check out the $_POST value before hand.
Back to top
View user's profile Send private message
edidur
Smarty Rookie


Joined: 21 May 2020
Posts: 12

PostPosted: Sat May 23, 2020 12:40 am    Post subject: Reply with quote

Is there any way I can assign the value in wcount internally to the FORM block so that it will be picked up by the action URL that i am building?
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sat May 23, 2020 8:17 am    Post subject: Reply with quote

Sorry, but could you elaborate?

Internally? Internal to what?

When POSTing, and your script having received the POSTed key/value pairs which now reside in the variable $_POST, you would then use Smarty's assign method to make that key/value available to the template:
PHP:
Code:
Smarty->assign("WCOUNT",$_POST['wcount']);

Template:
Code:
<form action="https://www.edidur.com/api.php?call=writingstats&user_id={$user->_data['user_id']}&word_count={$WCOUNT}&writing_date={$smarty.now|date_format:"%Y-%m-%d"}" method="post">

If you do not have access to the PHP scripts, then use $smarty.post.wcount instead of $WCOUNT.
Back to top
View user's profile Send private message
edidur
Smarty Rookie


Joined: 21 May 2020
Posts: 12

PostPosted: Sat May 23, 2020 1:37 pm    Post subject: Reply with quote

Bmither,
It is simply not working.

Code:

<form action="https://www.edidur.com/api.php?call=writingstats&user_id={$user->_data['user_id']}&word_count={$smarty.post.wcount}&writing_date={$smarty.now|date_format:"%Y-%m-%d"}" method="post" _self>
  <label for="wcount">Update Word Count:</label><br>
  <input type="text" id="wcount" name="wcount">
  <input type="submit" value="Submit">
</form>


Is still returning zero in wcount.

Further, using:

Code:
{$smarty->assign("WCOUNT",$_POST['wcount']);}


I am getting this error:

Code:
Fatal error: Uncaught --> Smarty Compiler: Syntax error in template "file:/var/www/edidur/content/themes/default/templates/_sidebar.tpl" on line 24 "{$smarty->assign("WCOUNT",$_POST['wcount']);}" $smarty. is not defined <-- thrown in /var/www/edidur/includes/libs/Smarty/sysplugins/smarty_internal_templatecompilerbase.php on line 24


(I know this is frustrating to help with, but man, I appreciate it!)
Back to top
View user's profile Send private message
edidur
Smarty Rookie


Joined: 21 May 2020
Posts: 12

PostPosted: Sat May 23, 2020 1:46 pm    Post subject: Reply with quote

I am wondering if I can have an html form in a tpl file that returns the name variable (In this case wcount)?

This works for other people?
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sat May 23, 2020 2:47 pm    Post subject: Reply with quote

The Smarty->assign() statement is in your PHP code, not the template. Once Smarty has the value from the assign method, one would display it using the named variable in the template.

When the page is being displayed by the browser, the page source will show you what the page has finally compiled to be. So, the <form> HTML will be all the values Smarty used to populate the variables.

What did Smarty replace {$smarty.post.wcount} with? (Again, this will be blank until after the first submission.)


Last edited by bsmither on Sat May 23, 2020 7:28 pm; edited 1 time in total
Back to top
View user's profile Send private message
edidur
Smarty Rookie


Joined: 21 May 2020
Posts: 12

PostPosted: Sat May 23, 2020 7:03 pm    Post subject: Reply with quote

What is being returned is 0 (zero) no matter the format I use.

here is a snippet of the api output:

Code:
{"id":"7470","user_id":"2","word_count":"0","writing_date":"2020-05-23","created":"2020-05-23 18:54:10","updated":"2020-05-23 18:54:10"}


Here is the code I have used to get that result (except the input on the submitted form was 1111)

Code:
<form action="https://www.edidur.com/api.php?call=writingstats&user_id={$user->_data['user_id']}&word_count={$smarty.post.wcount}&writing_date={$smarty.now|date_format:"%Y-%m-%d"}" method="post" _self>


Now if I pull out the {smarty.post.wcount} (or I have tried $_POST['wcount']) and hard code in that url api call, a word count (arbitrarily set to 1111) it will save to the database properly.

Like so:

calling action:

Code:

<form action="https://www.edidur.com/api.php?call=writingstats&user_id={$user->_data['user_id']}&word_count=1111&writing_date={$smarty.now|date_format:"%Y-%m-%d"}" method="post" _self>


with an api return of:

Code:
{"id":"7470","user_id":"2","word_count":"1111","writing_date":"2020-05-23","created":"2020-05-23 18:54:10","updated":"2020-05-23 18:59:50"}


So the fundamental issue seems to be, why is the HTML Form element of wcount not being assigned into the api calling url? (Why isn't the submitted number being stuffed into the url, like the user_is and writing_date is?)
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sat May 23, 2020 7:35 pm    Post subject: Reply with quote

Your browser probably has a Developer's Tools suite of functions. One tool is the Network panel. It is here you can see what actually gets sent out when you click the Submit button.

Use that tool to determine what key/value pairs are actually getting out via the querystring, and which key/value pairs are getting out in the POST payload.
Back to top
View user's profile Send private message
edidur
Smarty Rookie


Joined: 21 May 2020
Posts: 12

PostPosted: Sat May 23, 2020 7:55 pm    Post subject: Reply with quote

I have been looking at that, and the url that is being sent looks good, except that wcount (From the form) is being inserted instead of the value for wcount.

so the url looks like:

Code:

https://www.edidur.com/api.php?call=writingstats&user_id=1&word_count=wcount&writing_date=2020-05-23
Back to top
View user's profile Send private message
edidur
Smarty Rookie


Joined: 21 May 2020
Posts: 12

PostPosted: Sat May 23, 2020 8:46 pm    Post subject: Reply with quote

As an add-on piece of info, if I change the form method to "get" then run the form, the user_id is blank, wcount is accurate and the writing_date is also blank.

So the url that is built looks like:

Code:
https://www.edidur.com/api.php?wordcount=1234
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sat May 23, 2020 11:14 pm    Post subject: Reply with quote

The following is a quote taken from StackOverflow, making reference to the specifications on how browsers are to behave:
Quote:
As the specifications (RFC1866, page 46; HTML 4.x section 17.13.3) state:

If the method is "get" and the action is an HTTP URI, the user agent takes the value of action, appends a `?' to it, then appends the form data set, encoded using the "application/x-www-form-urlencoded" content type.

In HTML5, see http://www.w3.org/TR/2011/WD-html5-20110525/association-of-controls-and-forms.html#form-submission-algorithm

Look at "4.10.22.3 Form submission algorithm", step 17. In the case of a GET form to an http/s URI with a query string:

Let destination be a new URL that is equal to the action except that its <query> component is replaced by query (adding a U+003F QUESTION MARK character (?) if appropriate).

So, your browser will trash the existing "?..." part of your URI and replace it with a new one based on your form.


So, no, if passing back key/value pairs in the existing querystring is required, then POSTing the form is your only option (well, you could write a javascript interdiction).

You need to look at what is being POSTed prior to the page coming back to the browser. That would be on the Params tab of the Network pane when viewing the browser's POST action.
Back to top
View user's profile Send private message
edidur
Smarty Rookie


Joined: 21 May 2020
Posts: 12

PostPosted: Sun May 24, 2020 12:18 am    Post subject: Reply with quote

POST /api.php?call=writingstats&user_id=1&word_count=$_COOKIE[%27wordcount%27]&writing_date=2020-05-24 HTTP/1.1

This is what it is showing under params.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sun May 24, 2020 3:21 am    Post subject: Reply with quote

The $_COOKIE array is a PHP variable, usable in PHP code.

The template is not PHP code. The template is not executed as if it were PHP code.

Looking at the Params tab of the Network screen (highlighting the POST event), was there any mention in the POST section of the Params for the form element named 'wcount'? Did you enter a value in this text entry field prior to clicking the Submit button?
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP