Using $_GET to echo special characters from an URL - php

I am working on a school project and am quite new to php so pardon me if this may come off as sounding stupid.
I am trying to use $_GET to fill in a form with the information that was previously inputted into the fields when the user has somehow ran into a problem like leaving fields empty and was forced back into the form web page.
One of the fields may require the user to input operands on mathematical problems (ie. 1+1=2) but when echo-ing back the result the "+" sign is replaced with a space.
<input id="register" name="enun" type="text" class="form-control" placeholder="Question *" value="<?php if(isset($_GET['error'])){echo $_GET['enun'];}?>" />
The link "(...).php?error=equalquestion&enun=1+1=?&resp=2(...)" and I want to echo the bolded part.
I have tried some other fixes around stackoverflow like "htmlentities" / "htmlspecialchars" / "urldecode" but to no avail.
Thank you in advance!

Related

How to allow just data from an array into an input field?

for explanation i need this information for a project to handle Datamatrixcode - till this moment just everytime a number.
Okay i have a table with some numbers (unique_codes) and i take this numbers via SELECT these from a table (MySQL) and put them to an array ($row).
Scenario: the worker scans a number (and this numbers MUST BE a number from this $row list - then i $_POST this $row to other site to take it for the next step) otherwise i want a Error Exception.
My idea goes to us pattern like:
<form action="next site.php" method="post">
<input pattern="<?php $row['unique_code']; ?>"/>
<button class="btn btn-success">Send</button>
</form>
But this doesn´t work.
How can i solve this? Try and catch as an alternativ ?
EDIT - with a solution:
I let the input field to that what a input form does: receive input.
So i $_POST a number to the second page and proof this to my values.
I think you should either put print or echo in front of $row['unique_code'] or replace <?php with <?=, but then you have to enable short_open_tag in php.ini

Using data which contains ' and ", storing it in WordPress database & displaying it back in a meta box

I have some custom meta boxes on WordPress, storing some information such as page titles and descriptions, but I am having a bit of a problem which I can't wrap my head around.
The meta input boxes need to be able to accept " and ' (Speech marks and apostrophes), and WordPress is storing the data fine, and I can display it fine on the front end, but when it is echo'd back into the <input> box, it messes up because its trying to print something like this:
E.g: <input value="Hello we're called "example" and we suck" />
So no matter how I go about it, it's being printed in the page edit screen (once I save) like this:
or something to a similar effect. Because I need the use of both characters, I can't use either of them to wrap the attributes in as an easy fix.
I'm just having a bit of a brainfart but really can't figure out the logic behind a solution to solve this, because if I escape the characters, they will just get shown to the end user as Hello we're called "example" and we suck which will confuse them even more.
Encode with esc_attr(), example from the Codex:
echo '<input type="text" name="fname" value="' . esc_attr( $_POST['fname'] ) . '">';

User submissions from mozilla full of random line breaks

I've been building a site where users submit a logline and a synopsis for something. This works through a that is then submitted using $_POST, and an SQL query is used to put it into an SQL table. For some reason, ONLY IN MOZILLA FIREFOX (I've tested in chrome, IE, safari, and even on an ipad (Safari again)), by the time it gets to the SQL table it is full of random line breaks. When the submission is viewed on a different part of the site, no matter what the line breaks are there, so it is definitely a problem with Mozilla with the submission step.
So what is the issue with Mozilla? I would love any help on making this not happen, here is what is being submitted (abridged to include relevant pieces):
User enters value in this kind of a textarea:
<form id="submitform" name="submitform" action="submit.php" onsubmit="return validateSubmitForm(event)" method="post">
// some code
<textarea style="height:300px;width:800px;font-family:Arial;border:1px solid #a6a6a6;
background-color:#fff9eb;resize:none"
wrap="hard" size="1500" placeholder="1500 character limit..." maxlength="1500"
id="submitsummary" name="submitsummary" type="text"></textarea>
// some code
</form>
Then, after the form is submitted as a $_POST, I input it into the data table with basically this SQL query:
"INSERT INTO table (userid, header, synopsis)
VALUES(1, 1, " . htmlspecialchars($_POST["submitsummary"]) . ")"
Any thoughts on why it is that only Mozilla is being problematic? Also, any thoughts on how to get around it? Much appreciated!
wrap="hard" is known to have issues with Firefox. Remove that and check if the random line breaks disappear.
Also set the cols and rows attributes for the textbox as Firefox also uses this to determine proper wrapping.
I suspect your issue is wrap="hard". This requires you to use the cols attribute and it inserts newlines at the wrap points of text in the textarea.
Removing the wrap or setting wrap="soft" will result in the submitted data only having newlines where the user hit enter (or if the user pasted, only where the pasted data had newlines).

Remove %5B%5D from URL when submitting form

When I submit a form with multiple checkboxes that have the same name I get a URL that looks something like this:
www.mysite.com/search.php?myvalue%5B%5D=value1&myvalue%5B%5D=value2
Is there someway that I can remove the %5B%5D to make the URL "pretty", with something like htaccess?
Code:
<form>
<input type="checkbox" name="myvalue[]" value="value1">
<input type="checkbox" name="myvalue[]" value="value2">
</form>
Is there someway that I can remove the %5B%5D to make the URL "pretty", with something like htaccess?
No. The [] are reserved characters in URLs, so they definitely need to be URL-encoded.
If using POST is not an option, which makes sense given that it's a search form, your best bet is to just give them each a different name with a value of 1 or so.
<form>
<input type="checkbox" name="option1" value="1" />
<input type="checkbox" name="option2" value="1" />
</form>
Or, if you really insist in them having the same name, then you should be extracting the query string yourself instead of relying on the PHP specific feature of returning an array when obtaining a parameter with a [] suffix in the name.
$params = explode('&', $_SERVER['QUERY_STRING']);
foreach ($params as $param) {
$name_value = explode('=', $param);
$name = $name_value[0];
$value = $name_value[1];
// ... Collect them yourself.
}
This way you can just keep using the braceless name.
<form>
<input type="checkbox" name="option" value="option1" />
<input type="checkbox" name="option" value="option2" />
</form>
[ and ] are reserved characters in a URL, so the browser must encode them in order for the URL to work correctly. You cannot have these characters in a URL. Nor can you have any other reserved characters such as spaces, ampersands, etc. They will all be encoded automatically for you (in many cases, even if you type the URL into the browser manually).
If you need a "pretty URL" you can:
Not use a form at all; provide a link to a known "pretty" URL.
Accept the ugly URL, but redirect it immediately to the pretty URL in point 1 above.
Avoid using angle brackets at all in your field names (but this would mean a lot of changes to your back-end code too)
Use a POST method on the form, so that the field data doesn't show up on the URL at all (but this would mean you don't have a link the user can bookmark).
If you must "prettify" this URL, my suggestion would be option 2 above.
Frankly, though, I wouldn't worry about it. People get waaaay to stressed about "pretty" URLs. I don't really get why.
Very few people I know ever actually type in a URL longer than just a domain name.
If you're worried about SEO for this, don't -- the search engine bots know what ULR encoding is and can look past it.
The only other reason for wanting a "pretty" URL is so that it looks good if users share it via an email link or something. To be honest, if you're worried about URL prettyness for that and it's got form fields in it then it's already too ugly, with just the & and = signs all over the place. The encoded brackets really don't make it any worse.
So my honest answer is: don't sweat it. It's normal; ignore it; get on with more important parts of your web development work.
If that is really a problem for you, how about "merging" everything into a single param using some kind of separator like , (or whatever you want).
So, instead of having a URI like myvalue%5B%5D=value1&myvalue%5B%5D=value2, you would end up with a URI like myvalue=value1,value2.
This is just an idea, don't have the code right now, but you will need to do it with JS, and parse the param value on your backend (in order to have an array).

HTML checkbox form and HTTP URL

So, I have this HTML form:
<form id="search_form" class="form_wrapp"
accept-charset="utf-8" method="get" action="http://testing.com/results">
<input class="inputbox" type="text" name="search_query">
<input class="ic_search" type="submit" value="">
<input type="checkbox" value="checkbox1" name="search_filter[]">
<label for="Checkbox1">Checkbox1</label>
<input type="checkbox" value="checkbox2" name="search_filter[]">
<label for="Checkbox2">Checkbox2</label>
</form>
and it redirects to this URL upon submit with the 2 checkboxes checked
results?search_query=dreams&search_filter[]=checkbox1&search_filter[]=checkbox2
It works like this (inside codeigniter I get the data with $this->input->get('search_filter')), but my question is: I am doing something wrong inside the form, or this is how it's supposed to work?
And I mean about: &search_filter[]=checkbox1&search_filter[]=checkbox2. Shouldn't it be something like: &search_filter[]=checkbox1,checkbox2 ? And if not, how can I make it work like that?
If you want it in the comma format you can do the following:
$filters = (array) $this->input->get('search_filter');
$filters = implode(',',$filters);
If you want to alter the format in which the form is submitted, assuming jquery for js:
$('#search_form').submit(function() {
var $hidden = $('<input type="hidden" name="search_filter" />').appendTo($(this)),
$filters = $('input[name^=search_filter]'),
value = '';
//loop through the filters check if there checked and add them to the value
$hidden.val(value);
$filters.remove();
});
Of course if the user doesn't have js enabled it will submit natively
Am I doing something wrong inside the form, or this is how it's supposed to work?
That's how it's supposed to work. At least if you need to read query string with PHP, those brackets need to be there to read the whole query string without each search_filter value being overwritten by the next one.
And if not, how can I make it work like that?
If you have to, you can use a POST request instead, process the submission, and redirect to the URL of your choice with whatever query string you want.
From your comment:
I wanted to make the url like this &search_filter[]=checkbox1,checkbox2 just to make it a bit more "beautiful"
Don't worry about that, seriously. The only time this matters is when you're doing extreme SEO and you don't want two URLs that point to the same place. It's common practice in those cases to remove all unused keys and alphabetize them so that all URLs with query strings are consistent, but mangling them into something custom still isn't a part of that.
Besides that, don't fight against the behavior - work with it - it's not "broken" and making it "beautiful" won't matter to anyone, plus you'll have to guess/remember which pages process query strings the correct way, and which ones use your "custom" method.
I am doing something wrong inside the form, or this is how it's supposed to work?
That is how it is supposed to work
Shouldn't it be something like: &search_filter[]=checkbox1,checkbox2 ?
Then you couldn't tell the difference between two items and one item that had a comma in it.
And if not, how can I make it work like that?
Obtrusive JavaScript. Don't do that. Forms work well the way they work.
That's perfectly normal. form data is always sent in key=value pairs, with one single value. Submitting key=value,value is not part of the HTTP spec, and would have the values treated as a single monolithic string, not two separate comma-separated values.
You can certainly use some JS to rebuild your form on the fly to use the value,value format, but then you'll have to mod your server-side scripts to accept that new format as well. PHP won't auto-split the values before you, because it's not a standard representation.
&search_filter[]=checkbox1,checkbox2
Why you need this?
Use this like:
<?php
$searchFilter = $this->input->get('search_filter');
foreach($searchFilter as $filter)
// some actions with filters.
You search_filter[] is simple array with values from checkbox inputs.

Categories