Dynamicly generated html table with JQ datetimepicker to array - php

Please help..
Here's a dynamic 10-row html table with datetimepicker input area. I need to get all the datetime values but only the last cell data is available. What is wrong with the code?
<form action="atab.php" method="post">
<table>
...
while ($data=mysql_fetch_assoc($query)){
echo"<tr>";
...
<td><input class="datetimepicker" type="text" name="datet" />
<script>
$(function() {
$( ".datetimepicker" ).datetimepicker();
});
</script>
</td>
<?php
echo"</tr>";
}
}
echo'</table><input id="subm" class="btn btn-sm btn-success" type="submit" name="subm" value="Send" /></form><hr />';
...
and some php:
<?php
$date=array($_POST['datet']);
foreach ($date as $value){
echo $value;
}

You may have to declare the input name as an array type
<input class="datetimepicker" type="text" name="datet[]" />
then
$date=$_POST['datet'];
Also there is no need to initialize the plugin within the loop, move the below lines out of the loop
$(function() {
$( ".datetimepicker" ).datetimepicker();
});

replace
<input class="datetimepicker" type="text" name="datet" />
to
<input class="datetimepicker" type="text" name="datet[]" />
and try this:
<?php
foreach ($_POST['datet'] as $value){
echo $value."<br>";
}

Related

Hide a form field with a specific id after submit

I'm new here and a super noob in programming. I'm having trouble with my project. My problem is that I'd like hide the form after submit and retain the data input in it.
Here's my code:
<?php
$displayform = true;
if (isset($_POST['trck']))
{
$track = addslashes(strip_tags($_POST['tracknumber']));
$ord = $_POST['id'];
$displayform = false;
if (!$track)
echo "Please enter your tracking number!";
else
{
mysql_query("update `orderdetails` set `trackno`='$track' where `id`='$ord'");
}
if ($row2['id']==$ord)
echo $_POST['tracknumber'];
}
if ($displayform)
{
?>
<form method="post" action="">
<input type="text" name="tracknumber" id="tracknumber" size="30" maxlength="30" placeholder="Enter your track number here." />
<input type="hidden" name="id" value="<?php echo $row2['id']; ?>">
<input type="submit" name="trck" id="trck" value="Save" onclick="return confirm(\'Are you sure you want to save this tracking number?\');" />
</form>
</td>
</tr>
<?php
}
}
?>
This code was inside a while loop and my problem with this is that after I submit all the form is hidden. All I want is to hide the form with the specific ID on a query.
Simplest way is to use jQuery hide
Include the jquery as
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
$("#buttonid").click(function(){
$("#formid").hide();
});
You are looking for something like this in your <form> tag:
<form method="post" action="" id="awesome_form" onSubmit="document.getElementById('awesome_form').style.display = 'none';">
use the jQuery and use :
$("#submitButtonId").click(function(){
$('#formId').hide();
});
or else in pure javascript use
<input type="submit" value="submit" onClick="hideIt()"/>
<script type="text/javascript" >
function hideIt() {
document.getElementById('formId').style.display = 'none';
}
</script>
its better not to use inline javascript

dynamic inputs through jquery and submit to mysql table

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

rel attribute on <form> tag

Well, I have this form tag which works perfectly.
<form action="searchresult.php" method="post">
<?php
require('connect.inc');
$sql1="SELECT room_type,room_id FROM room";
$result1=mysql_query($sql1);
echo "<select name=roomtype value=''></option>";
while($nt=mysql_fetch_array($result1)){
echo "<option value=$nt[room_type]>$nt[room_type]</option>";
}
echo "</select>";
mysql_close();
?>
</td>
<td>
<input type='hidden' name='clientid' value="<?php echo $clientid; ?>">
<input type="text" class="w8em format-y-m-d highlight-days-67 range-low-today" name="chkin" id="sd" value="" maxlength="20" readonly />
</td>
<td>
<input type="text" class="w8em format-y-m-d highlight-days-67 range-low-today" name="chkout" id="ed" value="" maxlength="20" readonly />
</td>
</tr>
</table>
<input type='submit' name='submit' id="button1" value="Check Room Availabilty">
</form>
This opens up to a new page named "searchresult.php"
But,what I need to know is, how to display this .php file in a pop up style.
I am currently using facebox as my pop up,and I use it like this:
<a rel="facebox" href="searchresult.php">Check Room Availability</a>
But to no success, the inputs are not passing to the next page,so I get errors.
I know variables can be pass through the a tag like this
echo'<a rel="facebox" href=editroom.php?id=' . $row3["room_id"] . '>' . 'Edit' . '</a>';
But my case now is , I need the inputs to be pass.
I declare this codes in every page.
<link href="src/facebox.css" media="screen" rel="stylesheet" type="text/css" />
<script src="src/facebox.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('a[rel*=facebox]').facebox({
loadingImage : 'src/loading.gif',
closeImage : 'src/closelabel.png'
})
})
Thanks.
If you are using jQuery you could attach a method to the form submit to call
jQuery.facebox({ ajax: 'searchresult.php' });
You will ofcourse have to pass some URL parameters in order for it to display the relevant results
Something like this will work:
$('#search').submit(function() {
$.facebox({ ajax: 'searchresult.php?search=&search2=' });
return false;
});

PHP $_POST variable does not process input fields generated by Javascript

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" >

Get all multiple listbox values

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.

Categories