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

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

Related

Using $_GET to echo special characters from an URL

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!

PHP make daynamic variables and queries

So I hava a problem. On client side users insert theris data in textbox, radio in textarea. All number of input is stored in hidden type, sove php script on my server side knows how many input does it has. Sometimes there is just 20 inputs, sometimes 25 or 30, so the holl stuf is daynamic.
I have two questions:
1. How on server side dynamic generate variables and use them as $input1, $input2 and os on.
2. Let's say that I have somehow managde first problem so my second question is how to make query which sometimes uses only 20 parameters, sometimes 25 and so on. I don't wanna use arrays and tables;
I stareted php code:
for($i=1;$i<=$num; $i++){ //I get num from a hidden type
${"question".$i}="j";
if(isset($_POST["${"question".$i}"])){
${"question".$i}=$_POST[${"question".$i}];
echo question1; //this doesn't work but I want make created variables
//to use like this
}
else
{
echo "You have error with reading ".$i." question";
}
}
Change echo question1; by echo $question1; (append $ symbol before your var name)
Or in dynamic way:
echo ${"question" . $i}
Why would you like to use the variables like this?
If the input is dynamic use it like an array! -> Easier and cleaner.
There is good example how to handle a dynamic array input: Post array from html to php

how to chop up returned string from file_get_contents() into variable

i have a wordpress plugin that sends a request using file_get_contents() to a url and in-turn receive an image and four variables which ofcoz are already processed so they are just four words. How to i break down this string (using php) so i can take turn the four words into variable again as soon as they are returned, and use them on that page, (page C. )
here is a diagram of what i want to do
http://itmastersworld.com/my_problem.jpg
I have tried placing a form to be part of the string returned but apparently there is no way of manupilating the data inside the form
<form><input id="test" name="avariable" type="hidden" value="<?php echo $xxxx; ?>" /></form>
it works but i cant find a way to use the value="" without submiting the form from the server(using stuff like
$yes = $_REQUEST['avariable']
,) the form is introduced in part B that is, and appears in part c.
Help ???? I basically need my php variable created in part B.!!
Since you already have control on the page B, you can send them with some delimiter and split them? For example lets say the output is
word1,,,word2,,,word3,,,word4
Your code can be
<?php
$vars = explode(",,,", file_get_contents('your_url');
// echo $vars[1];
// echo $vars[2];
// echo $vars[3];
// echo $vars[4];
?>

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.

PHP pass a string containing quotations via GET

I am trying to pass a string that already contains quotation marks from one php file to another via a hyperlink and the GET method.
I am retrieving thousands of lines which contain quotation marks in a while loop and saving the output to a variable as follows:
while ($trouble_row = mysql_fetch_array($trouble_result)) {
$ticketid = $trouble_row['ticketid'];
$ticketno = $trouble_row['ticket_no'];
$created = $trouble_row['createdtime'];
$modified = $trouble_row['modifiedtime'];
$title = $trouble_row['title'];
$solution = $trouble_row['solution'];
$hoursattended = $trouble_row['cf_629'];
$hoursbilled = $trouble_row['cf_628'];
$csv .= "$firstname $lastname,$ticketno,$created,$modified,$hoursattended,$hoursbilled,$title,$solution\n";
}
The variable $title sometimes contains an entry that looks like this:
The user "tom" is having problems.
The variable $csv is collecting all the results from each pass and creating a CSV formatted string that I then need to pass to a new php script, which I am trying to do using a hyperlink:
a href="export_csv.php?csv=$csv">Export to CSV</a>
Unfortunately the embedded quotation marks are recognized by the hyperlink and cut off the majority of the output. Any suggestions on how to collect the data differently, store it differently, or pass it differently would be greatly appreciated!
For parameters in links, you need to use urlencode():
echo 'Export to CSV';
note however that GET requests have length limits starting in the 1-2k area (depending on browser and server).
Alternative approaches:
Forms
One method that is immune to length limits is creating a <form> element for each link with method="post" and adding the values in <input type='hidden'> inputs. You would then style the submit button of the form like a link.
<form action="export_csv.php" method="post">
<input type="hidden" name="csv" value=".......">
<button type="submit">Click here </button> <!-- Use CSS to style -->
</form>
Sessions
Another very elegant way to pass the data would be
Generating a random key
Saving the CSV data in a $_SESSION variable with the random key
Passing the random (short) key in the URL instead of the full data
You'd just have to take care of deleting unused random keys (and their data) frequently.
These kinds of links couldn't be bookmarked, of course.
Use urlencode() before creating the hyperlink url, and use urldecode() to get the original string.
use urlencode() for embedding into a link, and html_special_chars() for embedding into form fields.
url_encode and url_decode.
Quickest and easiest solution given what you already have is probably to change this:
Export to CSV
To something like this:
Export to CSV

Categories