We are building an advanced form with following fields...
<input type="text" size="50" name="name" >
<input type="text" size="50" name="custid" >
<input type="text" size="50" name="projectname" >
<input type="text" size="50" name="overdue" >
We are using jquery autocomplete to select name from MySQL and what we would like to do is:
We want to add custid, projectname, and overdue to their fields after adding name automatically, we can do it in PHP but how can we do it using jQuery or ajax? Can you help us to write up the code? We can add 1 output using ajax but how do we add multiple outputs? thanks.
You could retrieve the remaining fields using AJAX, by having a PHP script which outputs their details in JSON format. So if your data is stored in an associative array called $data, you can simply do echo json_encode( $data );. Each field name in the array, will be the name of the instance variable you'll need to call in the jQuery. The code below assumes your array field names are the same as your inputs.
You then use jQuery.get( ) (or similarly jQuery.ajax( )) to obtain the result. jQuery below:
$.get('myscript.php',
{name: name},
function(data) {
// If successful, fill out the fields
$('#custid').val(data.custid);
$('#projectname').val(data.projectname);
$('#overdue').val(data.overdue);
},
// tells jQuery it's retrieving a JSON encoded object
'json'
);
This will also require you to slightly alter your HTML as below:
<input type="text" size="50" name="custid" id="custid" >
<input type="text" size="50" name="projectname" id="projectname" >
<input type="text" size="50" name="overdue" id="overdue" >
Related
I have a form and there is a button to append another set of input boxes if you wish to add more information. Everytime it adds a new set of boxes all the input boxes get a unqiue number added on for that set of input boxes.
Example:
If you have three sets of input boxes it would look like this:
name, age, gender, dob
name1, age1, gender1, dob1
name2, age2, gender2, dob2
However, when I send this information over to my php file I extract the information from the array so each one is a variable. So, name would be $name and name1 would be $name1 and so on. But my question is how can I sanitize and validate all the names at once and all the ages at once etc..
The reason I am asking is because I have googled this alot and I can't find an answer on how to do this.
Try to create sets as given in sample below:
For first set:
<input type="text" name="name[]" id="name1" />
<input type="text" name="gender[]" id="gender1" />
<input type="text" name="age[]" id="age1" />
<input type="text" name="dob[]" id="dob1" />
For second set:
<input type="text" name="name[]" id="name2" />
<input type="text" name="gender[]" id="gender2" />
<input type="text" name="age[]" id="age2" />
<input type="text" name="dob[]" id="dob2" />
and set all the further sets accordingly.
Now, to get posted data you can use
<?php
echo "<pre>".print_r($_POST, true)."</pre>";
?>
You may use something like this for each entity:
<input type="text" name="age[]" id="age1" />
Here, id should be in incremental order with JavaScript or jQuery and name should be same which will give you an array for all the attributes in $_POST or $_REQUEST
Print $_REQUEST and you will come to know how exactly you can get all the data.
You are already getting the array for the name, age, etc.
To sanitize use array_map() function in php. It will sanitize the array.
EXAMPLE
$result = array_map("stripslashes", $result);
Here $result is an array
I have a form with multiple text inputs that all have the same name. How would I process that with my PHP when the user submits the form?
HTML:
<input type="text" name="interest"/>
I've assumed you're using POST.
You would use
<input type="text" name="interest[]">
Then on the post page, you could use:
foreach($_POST['interest'] as $i){
echo $i. "<br>";
}
or whichever method you wanted to use to get the POST data.
You could also do something like:
<input type="text" name="interest[music]"/>
<input type="text" name="interest[food]"/>
You can then call this data by using:
<?php echo $_POST['interest']['music']; ?>
<input type="text" name="interest[]"/>
You should add square brackets. This triggers PHP to put them in an array like this:
HTML
<input type="text" name="interest[]"/>
<input type="text" name="interest[]"/>
PHP
//Get values
var_dump($_POST['interest']);
Use brackets in your input field to create an array of POST values:
<input type="text" name="interest[]"/>
<?php
var_dump($_POST['interest']); // will be an array
?>
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.
I need to insert pairs of input values from a form into mysql database. For example here are my input boxes:
<input type="text" name="roomType1" size="30" />
<input type="text" name="roomRate1" size="30" />
<input type="text" name="roomType2" size="30" />
<input type="text" name="roomRate2" size="30" />
<input type="text" name="roomType3" size="30" />
<input type="text" name="roomRate3" size="30" />
etc..
And my sql database is set up as follows:
RoomType
RoomRate
HID
So basically I need to figure out how to pass the two input fields into each field together in the same row. I am not sure if I should do a for loop or how I can get each two and insert it with the same ID. I hope this makes sense. and any help would be GREATLY appreciated!
Those inputs must be part of a form with method="post". Then, on the php side use this:
<?php
$i = 1;
while(isset($_POST['roomType'.$i]))
{
$roomType = $_POST['roomType'$i];
$roomRate = $_POST['roomRate'$i];
// perform your sql statements here
$i++;
}
?>
Since your inputs are paired, it's enough to check if only one of them exists;
I have 3 text fields and I want to pass the values after combining them using a hyphen.
<input type="text" name="val[]" />
<input type="text" name="val[]" />
<input type="text" name="val[]" />
Preferably help me with php implode option.
How do i retrieve it after submit ?
Thanks.
After sending the form, your values will be in $_POST['val'] or $_GET['val'] as an array, depending on the method of your form.
You can combine them simply by:
$hyphenated = implode("-", $_POST['val']); // or $_GET['val']
thanks. how do i change focus to next field once a field has max values:
See if this works:
<input type="text" name="val[]" onkeyup='checkVals("field1", "field2");' id='field1'>
<input type="text" name="val[]" onkeyup='checkVals("field2", "field3");' id='field2'>
<input type="text" name="val[]" id='field3'>
<script>
function checkVals(this_field, next_field){
var fieldval = document.getElementById(this_field).value;
var fieldlen = fieldval.length;
if(fieldlen > 10){ // you can change 10 to something else
document.getElementById(next_field).focus();
document.getElementById(next_field).select();
}
}
</script>