How to post table with a form in php? - php

I have a form with a table (values in it can be edited) in it, like this:
<form action="file.php" id="myform" method="POST">
<input name="inp">
<div class="divclass">
<table id="mytable" name="mytable">
<tr><th>col1</th><th>col2</th></tr>
<tr><td contenteditable='true'>val11</td>
<td contenteditable='true'>val12</td></tr>
<tr><td contenteditable='true'>val21</td>
<td contenteditable='true'>val21</td></tr>
</table>
</div>
<input type='submit' name='submit' value='Submit'>
</form>
<?php
print_r ($_POST)
?>
So, when I click on submit, the entire form should be submitted right?
But just the inputs - inp & submit get submitted to the form, which I know when I print the post parameter array in php.
Can someone please explain how I can get the table data too in my post parameter array $_POST?
Also how do I trigger an onchange event on the table? I tried this, but it didn't seem to work :
$("#mytable").change(function (event) {
console.log("changed")
console.log($("#keno_config_table").val())
})

When you submit a form only the values inside selector elements gets submitted not all html elements.
However, if you can add an input tag inside <td> tag you'll be able to submit the values.The following code should work for you.
<form action="file.php" id="myform" method="POST">
<input name="inp">
<div class="divclass">
<table id="mytable" name="mytable">
<tr><th>col1</th><th>col2</th></tr>
<tr><td contenteditable='true'><input type="hidden" value="val11" name="value1"> val11</td>
<td contenteditable='true'><input type="hidden" value="val12" name="value2"> val12</td></tr>
<tr><td contenteditable='true'><input type="hidden" value="val21" name="value3"> val21</td>
<td contenteditable='true'><input type="hidden" value="val21" name="value4"> val21</td></tr>
</table>
</div>
<input type='submit' name='submit' value='Submit'>
</form>
jQuery
$("#mytable input[type='text']").change(function (event) {
console.log("changed")
console.log($(this).val())
});
PHP
<?php
if (!empty($_POST)){
echo "Post data received";
echo "Value 1 :".$_POST["value1"];
echo "Value 2 :".$_POST["value2"];
echo "Value 3 :".$_POST["value3"];
echo "Value 4 :".$_POST["value4"];
}else{
echo "Post data is empty. No value is passed from HTML to the server";
}
?>

So, when I click on submit, the entire form should be submitted right?
No. It isn't a form control.
But just the inputs - inp & submit get submitted to the form
inputs are form controls (as are selects, textareas and buttons).
Can someone please explain how I can get the table data too in my post parameter array $_POST?
Using JavaScript, listen for a submit event on the form element and copy the HTML of the table into the value property of a hidden input.
Alternatively: Use input elements in your table cells instead of using contentEditable.

I offer you to do this
<form action="file.php" id="myform" method="POST">
<input name="inp">
<div class="divclass">
<table id="mytable" name="mytable">
<tr>
<th>col1</th>
<th>col2</th>
</tr>
<tr>
<td><input name='col1[]' style='border:0;outline:0;display:inline-block' value='value'></td>
<td><input name='col2[]' style='border:0;outline:0;display:inline-block' value='value'></td>
</tr>
<tr>
<td><input name='col1[]' style='border:0;outline:0;display:inline-block' value='value'></td>
<td><input name='col2[]' style='border:0;outline:0;display:inline-block' value='value'></td>
</tr>
</table>
</div>
<input type='submit' name='submit' value='Submit'>
</form>
(of course, make a class for styles, I just wanted to do it simpler). Inputs will look like contenteditable tags without borders and you will be able to get an array of all col1 and col2 like $_POST['col1'] and $_POST['col2'] Also, you may change values to placeholders if you don't want default data to be sent.

Related

Can I submit user editable html form data to a PHP function using POST method?

A web page I'm working on has an HTML table that returns default values to the user as table data and these defaults are editable by the user on the page. I want to submit the inner HTML table data values to a php script using the POST method.
A few posts described using
<input type='hidden' name='name' value='abc'>abc</td>
which is fine when the data is static, but i cannot hardcode value='xxx' since the data is editable
My basic table data:
<form action='somescript.php' method='post'>
<td contenteditable='True'>
<input type='hidden' name='text'>
Editable default text
</td>
<button type="submit" class="btn">Submit</button>
</form>
Is there a way to set up the editable inner HTML to be sent using the POST method?
Try this:
<script type="text/javascript">
function setData(){
var test = document.getElementById("test").innerText;
document.getElementById("input-val").value = test;
}
</script>
<form action='somescript.php' method='post'>
<table>
<tr>
<td contenteditable='true' id="test">Editable default text<td>
<input type='hidden' name='text' id="input-val">
</tr>
</table>
<button type="submit" class="btn" onClick="setData()">Submit</button>
</form>
I hope this could help you.

How to send value of radio button to other page

I have a table with radio buttons to get the row values and 2 buttons
1 button.)For printing data , which moves to "notice.php"
2 button.)For row details,which stays on the same page.
<form action="" method="POST">
<table border="1" >
<tr>
<th>sourceID</th>
....
<th>Status</th>
</tr>
<tr>
<td><input type="radio" name="ID[]" value="<?php echo $tot; ?>" /></td>
<td>1</td>
....
<td>open</td>
</tr>
<input type="button" name="issue" value="Issue Notice" onClick="location.href='notice.php'" />
<input type="submit" name="details" value="details" />
<?php
if(isset($_POST['details']))
{
$n=$_POST['ID'];
$a=implode("</br>",$n);
echo$a;
}
Notice.php:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n=$_POST['ID'];
}?>
The problem here is: My code is working perfectly fine with the button details.
But it doesnt work with issue, i.e after selecting radio button and clicking on the issue notice button :it gives Undefined index: ID in D:\XAMPP\notice.php.
kindly help
Your details button is a submit button, so it submits the form. However your other button is just a regular button and you use javascript to send the browser to notice.php. As such, it does not post any data to notice.php.
You could include the data on the query string and send it that way, e.g.:
location.href="notice.php?id=<?=$tot?>"
Or you could also have the issue button post the page, and then have your receiving page check which submit button was used. If the issue button was used you could then have the php code post to notice.php.
Using the following code is the exact same as having a link:
<input type="button" name="issue" value="Issue Notice" onClick="location.href='notice.php'" />
As in, this will not change the form action and submit the POST data to your new page.
You would need something like:
<form method="post" action="" name="unique-form-name">
<input type="radio" name="ID[]" value="<?php echo $tot; ?>">
<input type="button" id="unique-btn-name" value="Issue Notice">
</form>
<script type="text/javascript">
document.getElementById('unique-btn-name').onclick = function(){
document['unique-form-name'].action='notice.php';
document['unique-form-name'].submit();
}
</script>
Then, once you get the data to notice.php, you'll have to use the data as an array (you won't be able to echo the data):
$IDs = $_POST['ID'];
echo '<pre>',print_r($IDs),'</pre>';
<input type="radio" name="ID" value="<?php echo $tot; ?>" />
Your error is the name attribute.
Also the other button is not related to the form at all. You may want to use ajax here.

Is this possible to accomplish with html and php?

I've made the form below. Is it possible to make it that when user enters the number of fields, for example 6, that the table below has 6 rows. It would be great if it would be possible to make it without any submit button (so that the trigger for this action is exiting from the text input box).
Here is the html code of this form:
<fieldset>
<legend>Student Information</legend>
Number of fields: <input type="text"><br />
Total number of characters: <input type="text">
<br>
<br>
<table border="1">
<th></th>
<th>field</th>
<th>number of characters</th>
<tr>
<td>1</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
<tr>
<td>2</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
</table>
</fieldset>
If this is not possible (without submit button), than in which way would you accomplish the same result? Thank you for any help and/or suggestions.
PHP is server side, it runs only once, when the page is loading. HTML is not a programming language. You could generate the table with PHP, but only if you had a submit button that reloaded the page. If it has to happen because of a user event, it always needs to be done with Javascript.
That means, you will need Javascript to make this work without reloading the page. Ideally, you would use Jquery (Javascript's most popular plugin) to manipulate the DOM.
If you had this input :
<input id="field" type="text">
You could call the on-leave event like this :
$("p").focusout(function()
{
// Delete the previous table, and create a new one, here
});
As for creating the actual table, it isn't complicated, but it is a bit of work. You should read the following reference to start you up :
http://www.tutorialspoint.com/jquery/jquery-dom.htm
You will need to "install" JQuery before-hand, you can simple insert this at the top of your code :
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
Okay here is the post only script you require
<?php
$rows=2;
if(isset($_POST['submit']))
{
if($_POST['submit']=='Update')
{
if(isset($_POST['rows'])) $rows=max($rows, intval($_POST['rows'])); // minimum 2 rows
}
else
{
// process posted data here
// reset post or jump to another page
$_POST=array();
//header("Location:index.php");
//exit();
}
}
?>
<form method="post">
<fieldset>
<legend>Student Information</legend>
Number of fields: <input type="text" name="rows" value="<?php echo $rows; ?>"><br />
Total number of characters: <input type="text">
<input type="submit" name="submit" value="Update"/>
<br>
<br>
<table border="1">
<th></th>
<th>field</th>
<th>number of characters</th>
<?php
for($loop=1;$loop<=$rows;$loop++)
{
echo '<tr>';
echo '<td>'.$loop.'</td>';
echo '<td><input name="field['.$loop.']" value="'.$_POST['field'][$loop].'" /></td>';
echo '<td><input name="chars['.$loop.']" value="'.$_POST['chars'][$loop].'" /></td>';
echo '</tr>';
}
?>
</table>
<input type="submit" name="submit" value="Submit"/>
</fieldset>
</form>
It will default to 2 rows (minimum), and retain the data when you update the rows.
If the rows get reduced, then the end ones disappear
It certainly would be doable with just PHP.
So for example, if you typed in '6' rows you could catch the form post and do something like (template form for within the HTML):
<?php for($i=0; $<=$_POST['rows'];$i++): ?>
<!-- This being your whatever html for the table -->
<tr><td></td></tr>
<?php endfor; ?>

Passing hidden values, html

I have a the following php file. displayitems.php
<?php
*
*
*
echo "<form action='http://retailthree.nn4m.co.uk/alex/add_data.html'>";
echo "<input type='hidden' name='value' value='$value'/>";
echo "<button type='submit'>Add</button>";
echo "</form>";
?>
Then the html file. add_data.html:
<form method="post" name="form">
<table id="mytable" border="1">
<tr>
<td>Trend <input type="text" name="trend" size="20"></td>
//many other fields
</tr>
</table>
</forn>
Then the aforementioned html will perform an action on a php file.
However that I want to achieve is to pass the hidden data ->$value from the first php file, to the Trend input box(to print the $value content to the input box). Is this possible?
You would simple use the posted variable and place it in the value attribute of your <input> like so:
<input type="text" value="<?php echo $_GET['value'] ?>" name="trend" size="20">
Of course you should do some validation before echoing it to the <input>
EDIT:
Quite rightly mentioned by #ocanal - GET is the default method for forms. You will not be able to process these forms with PHP if your file is *.html it must be a *.php file.
change the name of add_data.html file to add_data.php use following code in add_data.php file
<?php
// your php code
?>
<form method="post" name="form">
<table id="mytable" border="1">
<tr>
<td>
Trend <input type="text" name="trend" size="20"
value="<?php echo $_POST['trend'] ?>">
</td>
//many other fields
</tr>
</table>
</forn>
I'm a little lost but assuming you mean that you want the hidden value to appear in a text input field on another page I would suggest this:
HTML page
<form name='form' action='yourPhpFile.php' method='POST'>
<input name='hiddenGuy' type='hidden' value='hello from the hidden guy'/>
<input type='submit' value='Send'/>
</from>
Now for your php file called yourPhpFile.php
<?php
//your value from the hidden field will be held in the array $_POST from the previous document.
//the key depends on your field's name.
$val = $_POST['hiddenGuy'];
echo "<form name='form' action='yourPhpFile.php' method='POST'>
<input name='showingInText' type='text' value='".$val."'/>
</from>";
?>
This can be achieved on the same page too by removing the form action attribute. and echoing the a different input type and value depending on whether $_POST is set using the isset method.
if(isset($_POST['hiddenGuy'])){
echo "<input name='showingInText' type='text' value='".$_POST['hiddenGuy']."'/>";
}
else{
echo "<input name='hiddenGuy' type='hidden' value='hello from the hidden guy'/>
<input type='submit' value='Send'/>";
}

How to get textbox value in to another page... php / MySQL

I need to get textbox value to another php page. By clicking 'Box' icon i want to submit perticular row only. I already got row ID to 'Box' icon. How can i do that with the while loop.
thanks in advance
Tharindu
You should arrange the HTML code for this in the following fashion:
<table>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
</table>
Then once you hit the image, it will submit the form. Add this code to the top of your PHP script "<?php print_r($_POST); ?> and you will see that you can now process the posted contents based on the data that was posted without the need for any while loop.
let the you have posted be list.php
in the text box like
<input type=text name="rid" value= " <?php echo $rid ?>" onclick="location.href='view.php'"/>
get the row id in next page that is view.php
$id = $_GET['rid'];
pass it as hidden in view.php
<input type="hidden" name="id" value="<?php echo $id; ?> "/>
Make sure all your db connection goes perfect and echo all data whatever you want from row.
In the original php file generate a different html form for each box.
<form action="page2.php" method="post">
<input name="Z067DA" />
</form>
In the page2.php file use code similar to this. $value contains the user
submitted information.
foreach($_POST as $key=>$value)
{
// Use submitted value.
}
If you know the input tag names in advance you can just access them directly in your php code.
$value = $_POST['Z067DA'];

Categories