Passing in the correct values in an MVC Pattern - php

I am trying to use an HTML form to submit the url index.php?page1&name2=value2&name3=Value3. The closest I've come is index.php?page1=&name2=value2...
I can't figure out how to get a clean page1 and I'm certain there's a simple answer. I'm not sure where to search but the MVC pattern I implemented (John Squibb's) assumes that format, I have to assume it's possible. Any information is appreciated.
What I have so far:
<form id="page1" action="index.php" method="get">
<input type="hidden" name="page1"/>
...

A clean page1 variable does not make much sense in this context.
When you submit a FORM, the browser decides how to encode your form variables into a query string (the part after the ?) that the browser then presents to the HTTP server. I could not find a standards document in a quick google search, but the Wikipedia page is quite good.
So the browser is choosing to encode page1, which has no value as page1=. This is what I would expect any browser to do. When your query string is processed on the server side, just about every language (PHP, Ruby, Python, etc...) should be able to handle this and will create a page1 variable with either a null or empty string value. If you're processing the query string on the client side (say with Javascript), you should find a library capable of parsing the query string.
What are you trying to accomplish? Why do you require the equal sign to be missing?

Related

Display instantly value in textbox using php

I have a textbox where the value is depends on the input type into it.
<input name="convertedcurrency" id="convertedcurrency" type="text" required="required"/>
Is there any possibility that using any PHP code to display the value of the typed value instantly? (I was trying to echo the value using php at another textbox.)
P/S: It must be a php code due to I need this value to combine with other php value for other use.
There is nothing to do with php. You input is rendered and proceeded clientside (in the browser), so all manipulations should be done there too.
You should pass all the values and logic to the clientside, usually JavaScript, maybe with some sort of framework, jQuery, Angular.js, Ember.js, etc. Do manipulations there, and if you need to save something on the serverside, for example in the database - only then pass the data back using AJAX, and then you will need serverside processing with php, pyton, node.js, etc. Whatever is ok for you.

submitting form on one page urlencodes get values, doesn't on another page

I'm trying to sort out an issue with foreign characters and matching those to a database value.
I've managed to get a match out of the database query as I wanted but now I've run into a different problem and simply don't know why what's happening is happening.
On all pages throughout the site there is a header include which has a input field to search the site.
<form action="/search.php" method="get"><input name="q" type="text" />etc...
My problem query string was this grønhøj. When I enter this string into the input form on the homepage I get taken to the search page with the url like so: search.php?q=gr%F8nh%F8j which doesn't work at the moment.
However if I then re-enter that same search query into the header input when im on the search page the page reloads except the url now looks like this: search.php?q=grønhøj which does work.
If the resulting url would remain the same all the time, then I'd not have a problem, but because its inconsistent I don't know how to provide solutions to both possible versions of the query string.
So I guess I have 2 questions.
1) Why does the url not stay the same when it's using the exact same form to submit the string?
2) how can I manipulate both versions (or stop the different pages resulting in different urls) of the url so that the resulting string is consistent regardless of which version of the url I get?
UPDATE: I found a function to detect utf8 encoding Here which allowed me to switch how I handle the url string depending on which version of the url I get, so now my main issue is fixed.
I would still however like to understand why I get the 2 different url variables from the different pages even though the form is a consistent include across the site. Any ideas?
One way you can solve this issue, is to always decode the query string using urldecode() and then forcefully use urlencode() on it again. This way, if the initial query string was url encoded or decoded, no matter what, it will go through decoding and encoding process again, which will result in the same final query string.
Manual - urlencode
Manual - urldecode

how querystring works

This isn't the best question ever, but since search engines feel the need to ignore symbols, I have to ask somewhere.
In a link, I'll sometimes see a ?, such as [link]/file.extension?some_type_of_info, or even +,&,=, etc ('best example' of what I mean is youtube videos). What are these called and what do they do? A good site would be great to :)
I am mostly interested because I have a site that loads stuff into a page, and currently the way I allow 'bookmarking' a page (or more important to me, being able to go back a 'page') is use hash values to represent my 'page'.
Ultimately I would like to not have the page refresh, which is why hash values are good, but I'd like alternatives if any (not really what hashmarks are meant for, but mostly different browsers seem to treat assigning the hash values in jquery differently)
Again, sorry this is mostly just a "what is this" question, but if anyone could tell me pros/cons towards using the method in question versus hash values, that would be great also :)
See the url specification, in particular the section syntax components:
URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
… and the definition of query.
The query component contains non-hierarchical data that, along with
data in the path component (Section 3.3), serves to identify a
resource within the scope of the URI's scheme and naming authority
(if any). The query component is indicated by the first question
mark ("?") character and terminated by a number sign ("#") character
or by the end of the URI.
Ultimately I would like to not have the page refresh
Use the history API. This is independent of the structure of the URL (other than having to be a URL on the same origin).
The part after the ? is called query string. It's used to pass parameters to a web site. Parameters are separated using the & sign. For example, this would pass parameters to a site:
http://test.site.tld/index.php?parameter=value&another=anotherValue
This would pass the parameters "parameter" (with value "value") and the parameter "another" (with value "anotherValue") to the script index.php.
The + sign is sometimes used to represent a space. For example, "Hello World" could be represented as "Hello+World" or "Hello%20World".
A # sign is used to jump directly to an anchor within the page. For example
http://test.site.tld/index.php#docs
Would jump to the anchor "docs" within the web site.
The ? in a URL introduces the query string, which is data provided to the server. Everything prior to the ? specifies the resource on the server (in theory), and everything after it is additional data.
So for example:
http://example.com/foo/bar/page.php?data=one
http://example.com/foo/bar/page.php?data=two
Both URLs cause the page.php page to be retrieved by the server, and since it's a PHP page, a properly-configured server will run the PHP code within it. That PHP code can access the query string data as one big string via $_SERVER['QUERY_STRING'], or as a series of name/value pairs (if that's what it is, it doesn't have to be) via $_GET['paramname']. Note that that's _GET because query string parameters are GET parameters; POST parameters are sent via a different mechanism (not available for just links; you need a form or similar).
The stuff at the end of the url is a querystring. ? is used to denote the beginning of a querystring, they use key=value pairs seperated by &.
To address your question of whether this can be used for bookmarking, I believe the approach you are currently using with URL hashes (#) is correct.
The ? part is a querystring, GET parameters are sent that way.
The more interesting part of your question is: how can I enable the back-button/history for users in a dynamic website? Check out this library: https://github.com/browserstate/History.js/
It enables you (for newer browsers) to get/set history states. Each dynamic page gets it's own address. For older browsers, there is the hash-bang fallback (#/page/page).

PHP and post arrays

Is there anything bad in using post arrays for post variables?
<input type="text" id="stuff" name="stuff[text]"/>
instead of
<input type="text" id="stuff" name="stuff"/>
Tips when to use them?
No, there is no reason not to do this.
However, PHP is pretty much the only language which allows to create arrays like this - so if you ever change your backend to a different language you might have to change things.
It isn't a bad way. Typically programmers use array in such manner to post array with non-determined lenght. Still. Don't know for certain, but when You want to change method to GET then on IE <= 8 is a limit to 2048 chars in address lenght. And dynamic generated array can easy depleat this limit. On other browsers limit is much higher or there is none.
Another drawback of this method is that PHP will preceed correctly, but other server side languages may not. This isn't specified in official HTML docs, as far I know.
So it is more convinient to put it in a single cell in post array, that do subarray. If You want to do some namespacing, then You can write name in such way:
name="styff.text"
as do some of forum engines (for certain Vanilla 2 does).
If it has no diffrence to You I would stay to single variable name in html names. Mostly because of backend.
For tips how to use them I could recommend to use such array to cover dynamic generated content on site. Still it can be handled with normal names, but it is pretty ugly. If we have a case that You want to do a picture adding system, that I would name each input file with "pic[]" and on server iterate whole table.
The same thing for generating documents on client side. I would then do names like "content[][name]" "content[][type]" "content[][value]" and so on. Whathever I woud have in document part class I would throw in this kind of naming, and on server just check is set and do certain things for certain block of document.
This could be talked for a long time since every programmer have own technics and they tend to stick with it. For example I throw a in every form I have on site, and each action is parsed by a generall controller, and then passed by do specific controllers.
Nothing bad. If it is useful four you, use it.

Obtaining form input types in php

Is there a php equivalent to something like this jquery:
var allInputs = $(":input"); allInputs.attr('type');
I need to retrieve the types from each of the post variables sent to a php script but I want to do it without using javascript and/or jquery, I guess it would also be nice to get the other attributes as well (id, class etc). Perhaps I have missed something but I have tried to find the answer to this on the internet in search engines etc and can't even find another question similar to this one!
Thanks for any help and advice.
There is no way to do this without passing the types from jquery or javascript. All php knows is that some strings are coming in.
You can do something like this:
$.post('blah.php',{
var1: 'test',
var1Type: 'text'
...
});
You're pretty confused, PHP is a server side language, any value in the $_POST array is just a value (precisely a string). PHP doesn't have any idea of the input type, it's just an HTTP request with some data in its body.
To be clearer, this is what your webserver sees when you're doing a post request:
Name=Jonathan+Doe&Age=23&Formula=a+%2B+b+%3D%3D+13%25%21
PHP module reinterpret that input as key-values pairs, nothing more.
Everything is sent as a string. You have to know what are you using for and what types sent variables are and validate user input data.

Categories