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.
Related
Im trying to make a form with Laravel that send two sets of data.
my form is like this.
<form>
<fieldset method="POST" action"some url">
<label for="nameField">Name</label>
<input type="text" id="nameField" name="nameField">
<label for="phoneField">Name</label>
<input type="text" id="phoneField" name="phoneField">
<input class="button-primary" type="submit" value="Send">
</fieldset>
</form>
the first in put nameField will save to the table "names".
the phoneField will save to another table "phones".
Ok! the problem is I want to let user make as many phoneField as they want (with Javascript of course).
so i think the best way is i save nameField like this:
Name::create([ "name" => request("nameField")]);
but how about phoneField? how to save them? users can make about 10 field or more. is there any way to group the phoneFields as any array and send the array with request HTTP?
You can send group of phoneField. Use array for phoneField
<label for="phoneField">Name</label>
<input type="text" id="phoneField" name="phoneField[]">
And in your controller
$data = $request->all();
$phoneFields = $data['phoneField'];
foreach($phoneFields as $phoneField)
{
//implement to save phone number...
}
I'm new to MySQL and as a learning project I'd like to make a recipe database. I'd like to the user to be able to enter ingredients through a simple HTML form but I'm stuck in how to label the form so that I can enter several ingredients into the database at once.
I'd like to do something like this:
<form method="post" action="insert.php">
Ingredient 1: <input type="text" name="ingredient"><br />
Ingredient 2: <input type="text" name="ingredient"><br />
Ingredient 3: <input type="text" name="ingredient"><br />
<input type="submit" value="Submit">
</form>
When I do this, I add rows to the table but they're all empty. I know it's got something to do with me using "ingredient" (the table value where I want to add the ingredient name) several times in the form, but I just don't know how to solve it.
I would absolutely love some input on how to make it work.
write it like
Ingredient 1: <input type="text" name="ingredient[]"><br />
Ingredient 2: <input type="text" name="ingredient[]"><br />
and when you will get the REQUEST array in php, you will actually
get an array of names
like
$ing = $_POST['ingredient']; // $ing will be indexed array
You can't use same name for multiple textboxes like you have done. The last input box's value will overwrite all of the other ones' since they have the same name. Either you have to use different names for each input texts or define name as array like :
Ingredient 1: <input type="text" name="ingredient[]"><br />
Ingredient 2: <input type="text" name="ingredient[]"><br />
Ingredient 3: <input type="text" name="ingredient[]"><br />
So $_REQUEST['ingredient'] or $_POST['ingredient'] will be a normal PHP array from which you can get the value of each textbox like $_REQUEST['ingredient'][x] where x is some integer index, which is valid as long as count($_REQUEST['addCart']) > x.
Working Code :
index.html
<!DOCTYPE html>
<html>
<head>
<title> Recipes </title>
</head>
<body>
<form method="post" action="register.php">
Ingredient 1 : <input type="text" name="ing1"/><br/>
Ingredient 2 : <input type="text" name="ing2"/><br/>
Ingredient 3 : <input type="text" name="ing3"/><br/><br/>
<input type="submit" value="Enter" name="submit"/>
</form>
</body>
</html>
register.php
<?php
// coding to check database connection
$connection = mysqli_connect('localhost','root','adm','recipe');
/*
root - username
adm - passowrd
recipe - database name
*/
//checking whether submit button is clicked or not
if(isset($_POST['submit']))
{
// Escaping special char & getting the values we entered in form through POST method
$ing1 = mysqli_real_escape_string($connection,$_POST['ing1']);
$ing2 = mysqli_real_escape_string($connection,$_POST['ing2']);
$ing3 = mysqli_real_escape_string($connection,$_POST['ing3']);
//data is table name
$query = "INSERT into data VALUES('','$ing1','$ing2','$ing3')";
$result = mysqli_query($connection,$query);
echo "<p>Successfully Entered</p>";
?>
Click here to go to home
<?
}
?>
Create a database and name it as recipe and a table as data. Table data structure:
Output :
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
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>
<form action="test.php" method="post">
Name: <input type="text" name="fname" />
<input type="hidden" name="fname" value="test" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
How can I read the values of both the fields named fname?
On my action file(test.php) under $_POST, I am getting only hidden field value.
Is there any PHP setting through which I can read both values?
I believe you want to name the fields as:
Name: <input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
to make PHP understand them as an Array.
In case someone wants to do this and doesn't want to change the name of the form elements, or can't, there is still one way it can be done - you can parse the $_SERVER['QUERY_STRING'] or http_get_request_body() value directly.
It would be something like
$vals=explode('&',http_get_request_body());
$mypost=Array();
foreach ($vals as $val) {
list($key,$v)=explode('=',$val,2);
$v=urldecode($v);
$key=urldecode($key);
if ($mypost[$key]) $mypost[$key][]=$v;
else $mypost[$key]=Array($v);
}
This way $mypost ends up containing everything posted as an array of things that had that name (if there was just one thing with a given name, it will be an array with only one element, accessed with $mypost['element_name'][0]).
For doing the same with query strings, replace http_get_request_body() with $_SERVER['QUERY_STRING']
If you want to pass two form inputs with the same name, you need to make them an array. For example:
<input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
You can then access it using the following:
$_POST['fname'][0]
$_POST['fname'][1]
You might want to rethink whether you really need to use the same name though.
Solutions are
1) Try using different name for textbox and hidden value
2) Use an array as mentioned above for field name
3) Its not possible as the values will be overwritten if the names are same