I need some advice in the following code.
var count = 1;
$(function(){
$('#addmore').click(function(){
count += 1;
$('ul').append("<li><input type='text' id='hd"+count+"' name='hd"+count+"' value='heading' onClick='this.select();'> <input type='text' id='amount"+count+"' name='amount"+count+"' value='amount' onClick='this.select();'></li>");
// return false;
});
});
what exact php code should i use to submit all the input into my test table of mysql.
<button id="addmore">+</button>
<form id="myForm" name="myForm" action="" method="post" onSubmit="return false;">
<ul>
<li><input type="text" id="hd1" name="hd1" value="heading" onClick="this.select();">
<input type="text" id="amount1" name="amount1" value="amount" onClick="this.select();"></li>
</ul>
<input type="submit" value="Submit" id="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
echo "h";
};
?>
keeping in mind that i've test table with two fields 'heading' and 'amount'.
if(isset($_POST['submit'])){
foreach($_POST['hd'] as $hds){
echo $hds;
mysqli_query($con,"INSERT INTO test (hdg) VALUES ($hds)");
};
};
and its not adding data into my table
Use [] for the name attributes instead of the numbers:
<li><input type="text" id="hd1" name="hd[]" value="heading" onClick="this.select();">
On the server-side you will have an array of values.
$hd = $_POST['hd']; // Returns an array
Related
Hi I try to sum two data using AJAX and display it into hidden text input after that, I will pass the variable using PHP post method.
There are no problems in my jQuery AJAX code but the problem is after I submit the button, the value I retrieve from textbox total_rec[] is blank.
Here's my code below.
HTML:
<form method="post" action="<?php echo base_url() ?>user/recitem_insert">
<table>
<thead>
<tr><th>Test</th></tr></thead>
<tbody>
<tr><td>
<input type="hidden" name="last_item_rec[]" value="<?php echo $row->rec_qty; ?>">
<input type="text" name="item_rec[]" id="txt" disabled="">
<input type="hidden" name="total_rec[]" value="">
</td><tr>
</tbody>
</table>
</form>
JQUERY AJAX:
<script>
$(document).ready(function(){
$("input[name=item_rec\\[\\]]").on('keyup',function(){
var one = $(this).parents('tr').find('input[name=last_item_rec\\[\\]]').val();
var two = $(this).parents('tr').find('input[name=item_rec\\[\\]]').val();
sum = parseInt(one) + parseInt(two);
$(this).parents('tr').find('input[name=total_rec\\[\\]]').val(sum);
});
});
<script>
PHP CODE: (recitem_insert.php)
$total_rec = $_POST['total_rec'];
for($i=0;$i<sizeof($check);$i++){
for($j=0;$j<sizeof($total_rec);$j++){
$query=mysqli_query($con,"UPDATE tblstock
SET
rec_qty='$total_rec[$j]'
WHERE id = '$check[$i]'
")or die(mysqli_error($con));
}
}
As you told that the item contain 2 or more value. So you can use class instead of name.
HTML
<input type="hidden" class="last_item_rec" name="last_item_rec[]" value="23" />
<input type="text" class="item_rec" name="item_rec[]" id="txt" />
<input type="hidden" class="total_rec" name="total_rec[]" value="" />
jQuery
$(function(){
$(".item_rec").on('keyup',function(){
var one = $(this).parents('tr').find('.last_item_rec').val();
var two = $(this).parents('tr').find('.item_rec').val();
sum = parseInt(one) + parseInt(two);
console.log(sum);
$(this).parents('tr').find('.total_rec').val(sum);
});
});
I'm trying to submit two form field values to a PHP function, on the same page.
The function works perfect manually filling the two values.
<?PHP // Works as expected
echo "<br />"."<br />"."Write text file value: ".sav_newval_of(e, 45);
?>
On the form I must be missing something, or I have a syntax error. The web page doesn't fail to display. I found the below example here: A 1 Year old Stackoverflow post
<?php
if( isset($_GET['submit']) ) {
$val1 = htmlentities($_GET['val1']);
$val2 = htmlentities($_GET['val2']);
$result = sav_newval_of($val1, $val2);
}
?>
<?php if( isset($result) ) echo $result; //print the result of the form ?>
<form action="" method="get">
Input position a-s:
<input type="text" name="val1" id="val1"></input>
<br></br>
Input quantity value:
<input type="text" name="val2" id="val2"></input>
<br></br>
<input type="submit" value="send"></input>
</form>
Could it be the placement of my code on the form?
Any suggestions appreciated.
You need to name your submit button, or check for something else on your if(isset($_GET['submit'])) portion:
<form action="" method="get">
Input position a-s:
<input type="text" name="val1" id="val1" />
<br></br>
Input quantity value:
<input type="text" name="val2" id="val2" />
<br></br>
<input type="submit" name="submit" value="send" />
</form>
OR keep same form, but change php to:
<?php
if( isset($_GET['val1']) || isset($_GET['val2'])) {
$val1 = htmlentities($_GET['val1']);
$val2 = htmlentities($_GET['val2']);
$result = sav_newval_of($val1, $val2);
}
?>
You can you hidden field as:
<input type='hidden' name='action' value='add' >
And check on php by using isset function that form has been submitted.
I have 3 textfields and only one submit button to send all the data of the textfield at once. But how do I send the data of all three textfields at once in php?
<form>
for($x=0;$x<3;$x++){
<input type="text" name="name">
}
<input type="submit" name="submit">
</form>
Now I have a three fields inside a for loop and I have to extract data from all of them using single submit button.So how can I do that?
Using the 'name' attribute on an input allows you to do this for example
<form action='submit.php' method='post'>
<input type='text' name='one'></input>
<input type='text' name='two'></input>
<input type='text' name='three'></input>
<input type='submit' name='submit' value='Submit!' />
</form>
and in your PHP you would do something like this
<?php
if(isset($_POST['submit'])){
$inputOne = $_POST['one'];
$inputTwo = $_POST['two'];
$inputThree = $_POST['three'];
//Do whatever you want with them
}
?>
There are better ways of doing this, but this is probably the simplest to understand
If you want all the inputs to have the same name do this
<input type='text' name='textinput[]'></input>
Use that instead and loop through all of the inputs like so
<?php
foreach($_POST['textinput'] as $input){
//do something with $input
}
?>
You put them all inside the same <form> and make sure they have different values for the name attributes (or that the values end in []).
I believe what you are looking for is this Note the [ ] behind the name field
<form>
for($x=0;$x<3;$x++) {
<input type="text" name="name[]" />
}
<input type="submit" name="submit" />
</form>
Then to retrieve the values
$names = $_POST['name'];
foreach( $names as $name ) {
print $name;
}
Assuming I have the following form:
<form action="process.php">
<input type="checkbox" name="option1" value="oats"> Oats
<input type="checkbox" name="option2" value="beans"> Beans
<input type="hidden" name="parameter" value="a"/>
<input type="submit" value="Submit"/>
</form>
What would normally happen is after clicking on the URL, the browser redirects to:
process.php?option1=oats&option2=beans¶meter=a
How do I make it such that when the submit is clicked all the checkboxes end up as part of the "parameter", but separated by commas? So in other words it would be:
process.php?parameter=a,oats,beans
Best solution with minimal javascript/jquery/html is best if no html solution.
If you want several values in one parameter, you have to name it like parameter[]
f.e.:
<form action="process.php">
<input type="checkbox" name="parameter[]" value="oats"> Oats
<input type="checkbox" name="parameter[]" value="beans"> Beans
<input type="hidden" name="parameter[]" value="a"/>
<input type="submit" value="Submit"/>
</form>
More or less here the idea:
first form, 1 hidden input will catch all the vaules of second and void form
<script type="text/javascript">
function myfunc(){
$('#void input').each(function(id,elem){
b = $("#res").val();
if(b.length > 0){
$("#res").val( b + ',' + $(this).val() );
} else {
$("#res").val( $(this).val() );
}
});
alert("VAL "+$("#res").val());
$("#real").submit();
return false;
}
</script>
<form id="real" action="process.php">
<input id="res" type="hidden" name="parameter" value=""/>
</form>
<form id="void">
<input type="checkbox" name="option1" value="oats"> Oats
<input type="checkbox" name="option2" value="beans"> Beans
<input type="hidden" name="parameter" value="a"/>
<input type="submit" value="Submit" onClick="javascript:myfunc()"/>
</form>
add some fix, submit the right form. Tested on jsfiddle:
JsFiddel "working" example
ADD SINGLE FORM VERSION
You've to know the name of the field, and ignore the other params in the answer
<script type="text/javascript">
function myfunc(){
// can use more selector
$('#real input').each(function(id,elem){
// append the data to this field so no need to process it
if(this.id == 'res'){
return;
}
// here I concat the values
b = $("#res").val();
if(b.length > 0){
$("#res").val( b + ',' + $(this).val() );
} else {
$("#res").val( $(this).val() );
}
});
alert("VAL "+$("#res").val()); // remove me
$("#real").submit();
return false;
}
</script>
<form id='real' method='POST' action="#">
<input type="checkbox" name="option1" value="oats"> Oats
<input type="checkbox" name="option2" value="beans"> Beans
<input id="res" type="hidden" name="parameter" value="a"/>
<input type="submit" value="Submit" onClick="javascript:myfunc()"/>
</form>
There is a very useful method to use for dynamic data grouping.
<form action="file.php" method="post">
<?php for ( $i = 0; $i < count( $something ); $++ ) { ?>
<input type="checkbox" name="<?php $something[$i]; ?>[]" value="user_id"> Oats
<input type="checkbox" name="<?php $something[$i]; ?>[]" value="user_name"> Beans
<?php } ?>
<input type="submit" value="Submit"/>
</form>
I was really only interested in checkboxes that were actually checked - so I built myfunc() from the other examples like this:
function myfunc(){
$('#void input[type="checkbox"]').each(function(id,elem){
if ( ! $(this).is(':checked') ) return;
b = $("#res").val();
if(b.length > 0){
$("#res").val( b + ',' + $(this).val() );
} else {
$("#res").val( $(this).val() );
}
});
var stuff = $("#res").val();
if ( stuff.length < 1 ) {
alert('Please select at least one');
} else {
$("#real").submit();
}
}
I have a multiple selection listbox in which I insert items using javascript. At a certain point I need to get the values of all entries (both selected and unselected).
I'm currently using this code:
<form method="post" action="?page=test" name="something">
<select name="thelist[]" id="selOriginalWindow" size="5" multiple="multiple">
</select>
<input type="button" value="Добави" onclick="openInNewWindow();" />
<input type="submit" value="Get" />
</form>
<?
if ($_GET['page']=="test") {
$thelist=$_POST['thelist'];
var_dump($thelist);
}
?>
Javascript inserts the values, but PHP only gets the selected items' value. How do I get the value of all of the items in that listbox?
This did the trick:
function selectAll(selectBox,selectAll) {
if (typeof selectBox == "string") {
selectBox = document.getElementById(selectBox);
}
if (selectBox.type == "select-multiple") {
for (var i = 0; i < selectBox.options.length; i++) {
selectBox.options[i].selected = selectAll;
}
}
}
</script>
<form method="post" action="?page=test" name="something">
<select name="thelist[]" id="selOriginalWindow" size="5" multiple="multiple">
</select>
<input type="button" value="Добави" onclick="openInNewWindow();" />
<input type="submit" value="Get" onclick="selectAll('selOriginalWindow',true)" />
</form>
<?
if ($_GET['page']=="test") {
$thelist=$_POST['thelist'];
var_dump($thelist);
}
?>
The post variable will only contain those values you select, therefore if you want to use the same methodology to send ALL values you will need to add an additional field to your form which includes ALL the values. You can then read all values from this.
One way would be to use <input ='hidden' value='arrayofallvalues' name='allvalues'/>
So you could have the following:
<?
$select_values=array('value1', 'value2','value3');
?>
<form method="post" action="?page=test" name="something">
<select name="thelist[]" id="selOriginalWindow" size="5" multiple="multiple">
<?
for ( $i= 0; $i< count($select_values); $i++) {
echo "<option value=".$select_values[$i].">".$select_values[$i]."</option>";
}
?>
</select>
<input ='hidden' value='".implode(",",$select_values)."' name='allvalues'/>
<input type="button" value="Добави" onclick="openInNewWindow();" />
<input type="submit" value="Get" />
</form>
<?
if ($_GET['page']=="test") {
$thelist=$_POST['thelist'];
$all_select_values=explode(",",$_POST['allvalues']);
var_dump($thelist);
}
?>
What this will do is mean that every time the form submits, you can use explode() to create an array of the available values for the selct box.