mysql_query result available on second post? - php

I am using a dynamically generated query string to display results from a search form for some reports - there are 5 search fields and $query2 could contain nothing or 5 additional search values.
$query="SELECT * from employee_work where company_id='$company_id' $query2";
When I POST the form I get the data displayed on screen which is great. I also then am using TCPDF to offer a PDF download of the data. I currently also the class for TCPDF via POST:
if($_POST['PDF']) {
do the TCPDF stuff......
}
<div id="export-buttons">
<form name="export" method="post" action="">
<input type="submit" name="PDF" value="PDF" class="button pdf">
</div>
The problem is that when a user clicks on the PDF button the POST array now only contains the value for the submit button and NOT the $query2 data from earlier so when I use TCPDF it outputs all the data and ignores the $query2 part of the search string.
How can I address this so either the original $query2 data stays available OR have anther way of checking if the form button has been clicked without overwriting the contents of POST? Can I use javascript to do this?
Thanks
Jason

why dont you add hidden inputs that have the values of the criteria from the search? That way when the user posts the request for the PDF you get also those fiels and can use them (AFTER making sure the values are SAFE) in the query.
Other safer way is to store in the session an object or array with the parameters that made the list and then pass the identifier of that search as a hidden input to the PDF form, like:
$_SESSION['sc0000001'] = array('field1'=>'value1', 'field2'=>'value2', 'field3'=123);
...
<form>
<input type="hidden" name="sc" value="0000001" />
...
</form>
when you post the form you get the identifier of the search and create the query with the criteria assign to session...
EDITED:
html before posting list criteria:
<form>
<input type="text" name="filter1" value="" />
<input type="text" name="filter2" value="" />
<input type="text" name="filter3" value="" />
<input type="text" name="filter4" value="" />
<input type="text" name="filter5" value="" />
...
<input type="submit" value="go go go" />
</form>
PHP that gets the filters, builds query, gets results and stores in session.
$sql = " select * from table_name where 1 ";
$arrFilters = array();
for($i=1;isset($_POST['filter'.$i]) && trim($_POST['filter'.$i])!="";$i++) {
$arrFilters['filter'.$i] = mysql_real_escape_string($_POST['filter'.$i]);
$sql.=" AND filter".$i."=".$arrFilters['filter'.$i];
}
// here you should have the complete $sql with the filters supplied
// lets save this search, we are going to keep only the last search from this user in his session.
$_SESSION['listingFilters'] = $arrFilters;
HTML with search results and after the form to get pdf:
<form>
<input type="submit" value="get pdf" />
</form>
PHP:
After the post to get the pdf we go check if there are filters
$sql = " select * from table_name where 1 "; // basic query
// get the filters array from session
$arrFilters = isset($_SESSION['listingFilters']) ? $_SESSION['listingFilters'] : array();
foreach($arrFilters as $filter => $value) { // for each filter add it to the query
$sql.=" AND filter".$i."=".$arrFilters['filter'.$i];
}
// here you should have the complete $sql with the filters in session
// and can do your pdf magic
Add pepper and salt to your pleasure (you need to revise the code to work for you and maybe also the query if you are using text has filters)

Without seeing more code, this is probably because you have two "forms" on the HTML page, with one submit button in one form, and another in the other form.
<form>
<input name="1" />
<input type="submit" name="go"/>
</form>
<form>
<input type="submit" name="createPDF" />
</form>
Make sure you have all the fields/buttons inside the one form.
<form>
<input name="1" />
<input type="submit" name="go"/>
<input type="submit" name="createPDF" />
</form>

Related

Is it possible to use placeholders on <input type="submit">?

I'm currently working on at the displaying of information from a database. I was making a summary site where you can only see the important things of a table. After that i made the first element as an <input type="submit"> in a <form>, so u can click it and come to the detail site. My problem is now: The value of this input type has to be my ID, so i can query correctly on me detail site. I was wondering if it is possible to use something like a placeholder, so that the ID is the value, but on the input type is written other text.
My input:
<form method="post" action="Details.php">
<input type="submit" placeholder = "test" name="Overview" onclick="parent.location='Details.php'" value="<?php echo $data[$i1]; ?>">
</form>
How it currently looks
I want it that the input type still has the same value, but is displaying on the website something else like "test".
Greetings!
No, but buttons can have different values and labels.
<button name="foo" value="bar">baz</button>
Since you are using a form-tag per row, you can add a hidden input-field in the form and set the value of the submit-button to whatever you like.
<form method="post" action="Details.php">
<input type="hidden" name="id" value="<?php echo $data[$i1]; ?>" />
<input type="submit" name="Overview" value="test" />
</form>

PHP Read and Write field values without a postback

I want to create a hidden field in my HTML page, all good, just a simple <input> field. Then, I'm trying to read the value of that input field as the page loads the first time, so the closest I've gotten to that is using a $_GET statement. I'm simply trying to echo out the value so that I can see that it's working like such:
<input type="hidden" name="selection" value="fixtures">
<?php
echo $_GET['selection']
and that just gives me nothing, if I try:
echo "<h1>$_GET['selection']</h1>
then I get 0.
Question is - what <form> parameters you have? Did you submit form? Simples example for it will be:
//form.php
<form method="GET" action="next.php">
<input type="hidden" name"selection" value="fixtures" />
<input type="submit" name="submit_btn" value="Send" />
</form>
//next.php
echo '<h1>'.$_GET['selection'].'</h1>';
You can achieve same thing with:
Link

how to validate form inputs

I have two html forms:
one for entering new data which I need to do empty field validation also check the input against my table data (duplication).
the second is to update a one row of data that was entered by form number one and for this one I need to do name validation "like if name doesn't exists or doesn't match it gives error"
I found some examples online but I didn't understand them
maybe some on here can help
this is form one code :
<form action="http://localhost/wordpress/process.php" method="post" name="myForm">
Name <input id="name" type="text" name="name" />
Telephone <input id="telephone" type="text" name="telephone" />
Fax <input id="fax" type="text" name="fax" />
Web address <input id="webaddress" type="text" name="webaddress" />
State <input id="state" type="text" name="state" />
Address <input id="address" type="text" name="address" />
<input type="submit" name="submit" value="Submit" />
</form>
and this is form two :
<form action="http://localhost/wordpress/orgupdate.php" method="post" name="myForm">
<!-- Same Input fields as Form1 -->
<input type="submit" name= "submit" value="Update" />
</form>
thanks
I've created a Demo for you which you can check here.
1. For checking if input field is Empty, I've used following methods:
a) required = 'required' - Reference: Link.
b) jQuery: - fails if Javascript is disabled.
$("#submit").click(function() {
var name = jQuery.trim($('#name').val());
if(name == ''){
$(".err").text('Name can\'t be left empty.');
return false;
}
return true;
});
c) PHP's empty($_POST['name']);.
2. To check if Already an org exists with same name, after submitting and validating do,
"SELECT * FROM `table_name` WHERE `name` = $_POST['name'];"
If number of rows returned is > 0, then there's an organization that already exists.
3. For updating existing org details, follow two steps:
I. Provide a list of organization names fetched from db.
II. Select a name from the list and then edit.
While editing details, I've made name field as read-only so that You can use name field in where condition to write update query. But its not a correct method, you should make use of id (Primary key) to update a particular value.
I've made way for that also, you can achieve that by using input-type="hidden" to store the id and when you post the form you can retrieve it and use it in update query.
Useful Links:
1. Demo.
2. Download Source Code.

Get Search keyword in URL

<form action="">
<input placeholder="SEARCH" name="search_input" type="text"/>
<input type="submit" name="search_submit"/>
</form>
If people search by "Keyword Item" I want URL will be http://mydomain.com/search?keywords=Keyword%20Item
How can I do it? I know needs configure in form action, get etc.
Thanks in advance.
Update
When I am trying with this code
<form action="http://search.golfoutletsusa.com/search?" method="get">
<input placeholder="SEARCH" name="Keywords" type="text"/>
<input type="submit" name="search_submit"/>
</form>
The URL is: http://search.golfoutletsusa.com/search?Keywords=85&search_submit=Submit+Query
I just want "&search_submit=Submit+Query" will be removed from URL.
<?php echo $_GET["keywords"]; ?>
You will need to change the name of the text field from search_input to keywords though.
You should also consider using an id attribute along with the name. And as the other answer says, form action and method should be set correctly.
Solution:1
You can add the following code at the top of your search.php (or, whatever the processor file):
<?php
if(isset($_GET["search_submit"]))
{
$keywords = $_GET["Keywords"];
header("Location: search.php?Keywords=$keywords");
}
?>
OR
Solution:2
You can omit the name of submit button if it is not really necessary to give it a name. So instead of
<input type="submit" name="search_submit"/>
just use
<input type="submit" />
Set action of the form to search.php and method to get.
Then change the name of your input element to keywords.
But still the url won't be - http://mydomain.com/search?keywords=Keyword%20Item
It will be - http://mydomain.com/search.php?keywords=Keyword%20Item
Simply put everything in the form properties, only you have to select a file which will receive all this, /search will not suffice:
<form action="search.php" method="get">
<input type="text" name="keywords" placeholder="SEARCH" />
<input type="submit" name="submit" />
</form>
EDIT: In the search.php file you would get the variable contents with the get global variable like this:
$search_query = $_GET['keywords'];
After that you simply write the rest of the code to do the search... Note that this would lead to an URL like http://www.example.com/search.php?keywords=query

Store values from dynamically created inputs to a PHP variable

I have a page that allows the user to add and remove text fields to a form using JavaScript.
Text fields are named field1, field2, field3, etc. and depends on how many fields the user has added
I'm trying to store all the values from my text fields into one Php variable;
I understand that i need to store them into an array first and then use implode(), but how can i specify how many inputs there are within my Php code?
Usually the best way to approach this is to use array-named input, as shown in the following example in the PHP docs:
<form action="" method="post">
Nombre: <input type="text" name="personal[nombre]" /><br />
Email: <input type="text" name="personal[email]" /><br />
Cerveza: <br />
<select multiple name="cerveza[]">
<option value="warthog">Warthog</option>
<option value="guinness">Guinness</option>
<option value="stuttgarter">Stuttgarter Schwabenbräu</option>
</select><br />
<input type="submit" value="submit me!" />
</form>
You could use the very same name for each of the user added fields, as in:
<input type="text" id="field1" name="fields[]" />
<input type="text" id="field2" name="fields[]" />
And then just use implode as required:
$imploded_fields = implode(', ', $_POST['fields']);
There are many options:
You can use cookies. Use PHP $_COOKIE to get it. For help.
You can use html hidden input fields - <input type="hidden" value=""> and can store actual number of fields in it.
But, Diego Agulló is better one
You can create a hidden field and initialize it with 1 if on option is already open(field1). And increase the the value of counter while increasing the value of fields and vise verse. On submit you will find the total fields added.
Thanks

Categories