I have a form which is made dynamically with jquery ajax and input fields names values comes dynamically, I want to update these fields with php I am not sure how to do this.
when form is submitted I does not know what will be the name of input fields.
<form id="modalform" action="#" method="post">
<input type="text" name="46" class="form-control margin-top20 " value="selcetopt 1" />
<input type="text" name="50" class="form-control margin-top20 " value="selcetopt 2" />
<input type="text" name="56" class="form-control margin-top20 " value="selcetopt 3" />
<input type="text" name="66" class="form-control margin-top20 " value="selcetopt 4" />
<input type="text" name="96" class="form-control margin-top20 " value="selcetopt 5" />
<input type="submit" value="Update" name="submit" />
</form>
You can iterate over $_POST to get all input filed values, Based on your question I have added a sample code below
<?php
if(!empty($_POST)){
foreach($_POST as $key => $value){
// Preprocess $key which holds name of input field
// You can apply your logic to process value for an input $key here
// From your example it looks like name is a number so special case can check within a condition for $key as number
if(ctype_digit($key)){
// This will only get the value of all dynamic input fields if name is a number
}
}
}
I hope it helps you
See if that helps in any way, create a form like that
<form onsubmit="return submitForm(this);">
<input data-key="field-1" value="" />
<input data-key="field-2" value="" />
<input data-key="field-3" value="" />
...
<button type="submit">Submit form</button>
</form>
JQuery code:
var form_data = {};
function parseInputs(form) {
$(form).find("input").each(function() {
form_data[$(this).data("key")] = $(this).val();
})
return form_data;
}
function submitForm(form) {
var options = {
type: "POST",
url: "form_handling.php",
data: parseInputs(form),
success: function (response) {alert(response)}
};
$.ajax(options);
return false;
}
And form_handling.php would be:
<?php
foreach($_POST as $key => $value) {
echo $key . " = " . $value;
}
?>
Related
I'm trying to get a value from a form, then display it on posting of the form. I can get the value to appear in the second text field, once I have chosen an option using the Ajax Auto-Select, but how do I get that value shown stored into a variable for display on posting? This is what I have been trying -
if ($_POST['action'] == 'getentity') {
$value= $entity;
$content .= '<div>'.$value.' hello</div>';
}
<form method="post" action="?">
<input type="text" name="TownID_display" size="50" onkeyup="javascript:ajax_showOptions(this,\'getEntitiesByLetters\',event)">
<input type="text" name="TownID" id="TownID_display_hidden" value="'.$entity.'" />
<input type="hidden" name="action" value="getentity" />
<input type="submit" name="submit" value="Find"/>
Many thanks for any help.
Try
<input type="text" name="TownID" id="TownID_display_hidden" value="<?php $value = $entity; echo $entity; ?>" />
and its better to use like this
if ($_POST['action'] == 'getentity') {
$value= $_POST['TownID'];
$content .= '<div>'.$value.' hello</div>';
}
it should work.
I have a table callled tb_app with fields 'id','name','aic','batchcode'
example: field values '1','james','0001','1'
If type james in the textbox(name) corresponding values for other fields(aic,batchcode) should be displayed in textboxes. Is this possible? can anyone help me...I'm a php beginner ...Please!
Form code:
<form method="post">
<input type="text" name="names" id="query" />
<input type="text" name="aic" />
<input type="text" name="batchcode" />
<input type="submit" name="show" />
</form>
You can do this by Jquery ajax Try in this way by using your exact field names it might help you
Your jquery script should be something like this
function getvalues()
{
var selname = $("input[name='names']:text"").val();
$.ajax({ url: "getuserdata.php",
data: {"selname":selname},
type: 'post',
success: function(output) {
$("#aic").val(output);
$("#batchcode").val(output);
}
});
}
and your Html code
<input type="text" name="names" id="query" onblur="getvalues()"/>
<input type="text" name="aic" id="aic"/>
<input type="text" name="batchcode" id="batchcode" />
The getuserdata.php
<?php
include("database connection file here");
$selname = $_POST['selname'];
$query = "SELECT * FROM table_name WHERE youruser_id = $selname";
$res = mysql_query($query);
$rows = mysql_fetch_array($res);
echo $rows['aic'];
echo $rows['batchcode'];
?>
I have seen this done before but not sure how.
I am trying to have a search form go to pagename.php?q=[searchquery] so i can then get the searchquery from the address.
here is the form
<form class="sidebar-search">
<div class="input-box">
<input type="text" placeholder="Quick Product Search..." />
<input type="button" class="submit" value="" />
</div>
</form>
Here is the JS
// handle the search query submit on enter press
$('.sidebar-search input').keypress(function (e) {
if (e.which == 13) {
window.location.href = "search_results.php";
return false; //<---- Add this line
}
});
// handle the search submit
$('.sidebar-search .submit').click(function () {
if ($('.page-container').hasClass("sidebar-closed")) {
if ($('.sidebar-search').hasClass('open') == false) {
$('.sidebar-search').addClass("open");
} else {
window.location.href = "search_results.php";
}
} else {
window.location.href = "search_results.php";
}
});
Can anyone help with this?
You wouldn't actually need to do it using javascript
<form class="sidebar-search" method="get" action="search_results.php">
<div class="input-box">
<input type="text" placeholder="Quick Product Search..." />
<input type="submit" class="submit" value="" />
</div>
</form>
The action attibute defines the location (an URL) where the form's collected data should be sent.
The method attribute defines which HTTP method to send the data with (it can be "get" or "post").
This would probably help understand in detail.
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/My_first_HTML_form?redirectlocale=en-US&redirectslug=HTML%2FForms%2FMy_first_HTML_form
But if you still need to use javascript here the answer
define an id for the search field as below
<input type="text" id="txtSearch" placeholder="Quick Product Search..." />
and then
var searchString = $('#txtSearch').val();
window.location.href = "search_results.php?q=" + searchString;
Do this:
window.location.href = "search_results.php?q=" + $(".sidebar-search input[type=text]").val();
Or you can give an id to the search and get the value of the element with that ID.
if you hit enter/submit you go to search.php?query=value
<form method="get" action="search.php" >
<input name="query" type="text" />
<input type="submit" value="search" />
</form>
I have two files, (file1 and file2). file1 includes file2 in a PHP include statement. File1 also contains a form and prints out all $_POST variables. File2 uses a Javascript button to dynamically change the value in an input field. The problem is that $_POST is empty after submit is pressed. Why is that and how do I fix it?
File1:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php include 'file2.php'; ?>
<input type="submit" /></form>
<?php foreach ($_POST as $key => $val) { echo $key . " belongs to " . $val; } ?>
File2
<script type="text/javascript">
var button = {
counter : 0,
count : function() {
text = document.getElementById("text");
this.counter++;
text.setAttribute("value", this.counter);
}
};
</script>
<button type="button" onclick="button.count()">CLICK ME!</button>
<input id="text" type="text" value="0" />
You forgot to set name attribute:
<input id="text" name="text" type="text" value="0" />
text.setAttribute("value", this.counter);
This is better off as:
text.value = this.counter;
Also you need a name attribute on your element:
<input id="text" name="text" type="text" value="0" />
you didnt set the name attribute for the field "text".
Corrected code:
<input id="text" name="text" type="text" value="0" >
I am creating a dynamic form where the user will have the ability to add a set of inputs to the form. The html looks like this:
<form>
<input id="title1" class="title" name="title1" type="text" value="">
<input id="productionCompany1" name="productionCompany1" type="text" value="">
<input id="year1" name="year1" type="text" value="">
<input id="role1" name="role1" type="text" value="">
<div id="newCredit"> </div>
add another credit
</form>
When the user clicks the link with the id of "addCredit" the following jQuery script is called:
$(document).ready(function() {
var $ac = $('#addCredit');
$ac.click(function() {
/* the following two lines are where the problem lies? */
var $credInt = $(this).prev(".title");
$.get("addCredit.php", {num: $credInt},
function(data){
$('#newCredit').append(data);});
return false;
});
});
The jQuery function queries a php file called "addCredit.php", which looks like this:
<?php
$int = $_GET["num"];
$int = substr($int, -1);
$int++;
?>
<input id="title<?php echo $int;?>" class="title" name="title<?php echo $int;?>" type="text" value="">
<input id="productionCompany<?php echo $int;?>" name="productionCompany<?php echo $int;?>" type="text" value="">
<input id="year<?php echo $int;?>" name="year<?php echo $int;?>" type="text" value="">
<input id="role<?php echo $int;?>" name="role<?php echo $int;?>" type="text" value="">
My problem is getting the javascript variable $credInt set properly so that it can be sent to the addCredit.php page and update the form fields accordingly. I also need to be sure that every time the form is appended, the next value sent is the incremented value.
Any thoughts on how I might accomplish this? Thank you for your help.
This is the wrong way of doing it; PHP can handle array syntax in a variable name. This makes it much easier to handle. It is also unnecessary to call the server to clone the form. You should name your fields like this:
<form>
<div id="originalCredit">
<input name="title[]" type="text" value="">
<input name="productionCompany[]" type="text" value="">
<input name="year[]" type="text" value="">
<input name="role[]" type="text" value="">
</div>
add another credit
</form>
And then your Javascript can be like this:
$(function() {
$('#addCredit').click(function() {
var newCredit = $('#originalCredit').clone(); // create new set
newCredit.find('input').val(''); // empty input fields
$(this).before(newCredit); // append at the end
return false;
});
});
When the form is finally sent to the server, because the variables are in the format of name[] PHP will recognize they are an array and then you can do this:
<? foreach($_POST['title'] as $k => $v) { ?>
Title: <?=$_POST['title'][$k]?><br>
Company: <?=$_POST['productionCompany'][$k]?><br>
Year: <?=$_POST['year'][$k]?><br>
Role: <?=$_POST['role'][$k]?><br>
<? } ?>
Obviously this is just displaying it as an example, but you can then save/update/whatever with it.