Im trying to create some forms using php. I was asked to make a dynamic form that adds input rows to a table on the click of a button. For some reason I cannot print the values stored in an array for each dynamic form row using php.
<html>
<body>
Welcome <?php echo $_POST["name"]; ?></br>
You e-mail address is <?php echo $_POST["email"];?></br>
Your occupation is <?php echo $_POST["occupation"];?></br>
<?php foreach($_POST['colleague' as $a){ ?>
Your colleague is <?php echo $a;?></br>
<?php }?>
</body>
</html>
The php code should print all the colleague values that were submitted in the form, but for some reason it is not doing it.
<html>
<script src="script.js"></script>
<body>
<form action="test.php" method="post">
<table id="testTable" style="text-align:left">
<tr>
<th>
Name
</th>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<th>
Email
</th>
<td>
<input type="text" name="email"
</td>
</tr>
<tr>
<th>
Occupation
</th>
<td>
<input type = "text" name="occupation">
</td>
</tr>
<tr>
<th>
Colleague
</th>
<td>
<input type ="text" name="colleague[]">
</td>
<td>
<input type="button" value="Add Colleague" onClick="addRow('testTable')" />
</td>
</tr>
</table>
<input type="submit">
</form>
</body>
</html>
'
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var header = table.createTHead();
var row = table.insertRow(rowCount);
var cell = row.insertCell(0);
header.innerHTML = table.rows[3].cells[0].innerHTML;
cell.appendChild(header);
var cellTwo = row.insertCell(1);
cellTwo.innerHTML = table.rows[3].cells[1].innerHTML;
}
Here, try this for HTML
<html>
<script src="script.js"></script>
<body>
<form action="test.php" method="post">
<table id="testTable" style="text-align:left">
<tr>
<th>
Name
</th>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<th>
Email
</th>
<td>
<input type="text" name="email">
</td>
</tr>
<tr>
<th>
Occupation
</th>
<td>
<input type = "text" name="occupation">
</td>
</tr>
<tr>
<th>
Colleague
</th>
<td>
<input type ="text" name="colleague[]">
</td>
<td>
<input type="button" value="Add Colleague" onClick="addRow('testTable')" />
</td>
</tr>
</table>
<input type="submit">
</form>
</body>
</html>
For the PHP file (test.php) write
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br />
You e-mail address is <?php echo $_POST["email"];?><br />
Your occupation is <?php echo $_POST["occupation"];?><br/>
<?php foreach($_POST['colleague'] as $a){ ?>
Your colleague is <?php echo $a;?></br>
<?php }?>
</body>
</html>
Hope it helps.
Related
I have created a PHP page where there is a form which would store data on an XML file.
I have written the code of PHP which could store the data but now I have to redirect it to my homepage also.
But the main error is when I write the Javascript code for redirecting the page, then the data is not stored.
Now I have currently written the code of Javascript in the PHP code itself.
I will provide the code for my page below.
<html>
<head>
<title>Login</title>
</head>
<body bgcolor="#f0f0f0">
<?php
if(isset($_REQUEST['ok']))
{
$xml = new DOMDocument("1.0","UTF-8");
$xml->load("a.xml");
$rootTag = $xml->getElementsByTagName("document")->item(0);
$dataTag = $xml->createElement("data");
$aTag=$xml->createElement("a",$_REQUEST['a']);
$bTag=$xml->createElement("b",$_REQUEST['b']);
$dataTag->appendChild($aTag);
$dataTag->appendChild($bTag);
$rootTag->appendChild($dataTag);
$xml->save("a.xml");
}
echo "<script type=\"text/javascript\">
function Home()
{
window.location.href=\"navbar.php\";
}
</script>
"
?>
<form action="index.php" method="post">
<table cellspacing="20px" style="margin-left:500px;" id="atable">
<tr>
<td>User Name </td>
<td> <input type="text" name="a" width="75" /> </td> <!--Inserting textbox in the page with justified width-->
</tr>
<tr>
<td> Password </td>
<td> <input type="password" name="b" width="75" /> </td> <!--Inserting textbox in the page with justified width-->
</tr>
<tr>
<td>
<input type="button" name="ok" value="Login" onclick="Home()" />
</td>
</tr>
</table>
</form>
</body>
</html>
Try this. You need to use header("Location:navbar.php");
<?php
if(isset($_REQUEST['ok'])){
$xml = new DOMDocument("1.0","UTF-8");
$xml->load("a.xml");
$rootTag = $xml->getElementsByTagName("document")->item(0);
$dataTag = $xml->createElement("data");
$aTag=$xml->createElement("a",$_REQUEST['a']);
$bTag=$xml->createElement("b",$_REQUEST['b']);
$dataTag->appendChild($aTag);
$dataTag->appendChild($bTag);
$rootTag->appendChild($dataTag);
$xml->save("a.xml");
header("Location:navbar.php");
exit;
}
?>
<html>
<head>
<title>Login</title>
</head>
<body bgcolor="#f0f0f0">
<form action="index.php" method="post">
<table cellspacing="20px" style="margin-left:500px;" id="atable">
<tr>
<td>
User Name </td>
<td> <input type="text" name="a" width="75" /> </td> <!--Inserting textbox in the page with justified width-->
</tr>
<tr>
<td> Password </td>
<td> <input type="password" name="b" width="75" /> </td> <!--Inserting textbox in the page with justified width-->
</tr>
<tr>
<td>
<input type="submit" name="ok" value="Login" />
</td>
</tr>
</table>
</form>
</body>
</html>
The Result of echo is always:
"Course Description Value is: ...Thanks"
with an empty value for Course_Desc
<?
if (isset($_REQUEST["saveEdites"])) {
$id = $_REQUEST['ProtocolID'];
$Course_Desc = $_REQUEST['Course_Descr'];
$Course_Desc = trim($Course_Desc);
$Course_Desc = stripslashes($Course_Desc);
$Course_Desc = htmlspecialchars($Course_Desc);
echo "Course Description Value is : ".$Course_Desc." ...Thanks";
}
?>
<form method="post" name="implantForm">
<table >
<input type="hidden" name="ProtocolID" id="Protocol">
<tr align="Left">
<td>
<label style="color:#ff6600;font-weight:bold">
Name
</label>
</td>
<td>
<label id="formLbl"></label>
</td>
</tr>
<tr align="Left" style="color:#ff6600;font-weight:bold">
<td>Protocol </td>
<td>
<textarea id="formTXT" rows="4" cols="50" name="Course_Descr" form="implantForm"></textarea>
</td>
</tr>
</table>
<table>
<tr align="center" >
<td>
<input type="submit" name="saveEdites" value="Save changes">
</td>
</tr>
</table>
</form>
Update
I was declaring the form="implantForm"
so I removed it and everything is working now
Please put <form> tag in your program and remove form=implantForm from <textarea>.Here I made some changes in your code then I get textarea value as output
<form>
<table >
<input type="hidden" name="ProtocolID" id="Protocol">
<tr align="Left">
<td>
<label style="color:#ff6600;font-weight:bold">
Name
</label>
</td>
<td>
<label id="formLbl"></label>
</td>
</tr>
<tr align="Left" style="color:#ff6600;font-weight:bold">
<td>Protocol </td>
<td>
<textarea id="formTXT" rows="4" cols="50" name="Course_Descr" ></textarea>
</td>
</tr>
</table>
<table>
<tr align="center" >
<td>
<input type="submit" name="saveEdites" value="Save changes">
</td>
</tr>
</table>
</form>
<?php
if (isset($_REQUEST["saveEdites"])) {
$id = $_REQUEST['ProtocolID'];
$Course_Desc = $_REQUEST['Course_Descr'];
$Course_Desc = trim($Course_Desc);
$Course_Desc = stripslashes($Course_Desc);
$Course_Desc = htmlspecialchars($Course_Desc);
echo "Course Description Value is : ".$Course_Desc." ...Thanks";
}
?>
form="implantForm"
that what was confusing the browser
textarea value was not even sent in the request .removed it and everything is working now
Please remove form="implantForm" from textarea, then it will work.
<?php
$con = mysql_connect('localhost','root','');
$db = mysql_select_db("demo",$con);
?>
<!doctype html>
<html>
<head>
<title>Demo</title>
<!--JAVASCRIPT -->
<script src="http://code.jquery.com/jquery-1.9.1.min.js" ></script>
<script>
$(document).ready(function(){
$("#myTable td").click(function() {
var column_num = parseInt( $(this).index() ) + 1;
var row_num = parseInt( $(this).parent().index() )+1;
//$("#result").html( "Row_num =" + row_num + " , Column_num ="+ column_num );
alert(row_num);
});
});
</script>
</head>
<body>
<!--<div id="result"> </div>-->
<table id="myTable" border="1" style="border-collapse: collapse;" cellpadding="8">
<tr>
<th>Name</th>
<th>Qty</th>
<th>Amount</th>
<th>Total</th>
</tr>
<?php
$sQ = "select * from tbl_demo";
$eQ = mysql_query($sQ);
while($fQ=mysql_fetch_array($eQ))
{
$name = $fQ['Name'];
$qty = $fQ['Qty'];
$amt = $fQ['Amount'];
//$total
$total = $fQ['Total'];
?>
<tr>
<td><input type="text" name="name" value="<?php echo $name; ?>" onchange="add($qty,$amt)"/> </td>
<td><input type="number" name="name" value="<?php echo $qty; ?>"/></td>
<td><input type="number" name="name" value="<?php echo $amt; ?>"/></td>
<td><input type="number" name="name" value="<?php echo $total; ?>"/></td>
</tr>
<?php
}
?>
<tr>
<td colspan="3" style="padding-left:400px"> Total </td>
<td class="navigateTest"> <input type="number" name="name" value=""/> </td>
</tr>
<tr>
<td colspan="4" class="navigateTest"> <input type="button" name="submit" id="submit" value="Submit"/> </td>
</tr>
</table>
</body>
</html>
Here is my code. I get index value of particular row nd column bt i can not find value of that cell.. I tried to solve that problem bt i can not understand code which i found..which are the different ways for that??
How i get data of particular cell?????
plz redirect me at right direction...
I have my table named i_table with columns:
id name req_qty req_date rcv_qty rec_date
1 metal 1 2014-03-04
2 spring 5 2014-03-04
in my html/php:
<form action="insert.php" method="post">
<?php $resource2=mysql_query("SELECT * FROM i_table",$con);
while($result2=mysql_fetch_array($resource2))
{
?>
<table>
<tr>
<td>
<input type="text" name="id" value="<?php echo $result2['id'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="name" value="<?php echo $result2['name'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="rcv[]" value="" />
</td>
</tr>
<tr>
<td>
<input type="date" name="rcv_date[]" value="" />
</td>
</tr>
</table>
<?php };?>
</form>
how can I insert in multiple arrays from an input according to their unique ID from db? pls help me here... huhu
In your form, add the id as the key -
<form action="insert.php" method="post">
<?php $resource2=mysql_query("SELECT * FROM i_table",$con);
while($result2=mysql_fetch_array($resource2))
{
?>
<table>
<tr>
<td>
<input type="text" name="id[<?php echo $result2['id'];?>]" value="<?php echo $result2['id'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="name[<?php echo $result2['id'];?>]" value="<?php echo $result2['name'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="rcv[<?php echo $result2['id'];?>]" value="" />
</td>
</tr>
<tr>
<td>
<input type="date" name="rcv_date[<?php echo $result2['id'];?>]" value="" />
</td>
</tr>
</table>
<?php };?>
</form>
Then on your insert.php where you post your form, get the id in your foreach -
<?php
if(isset($_POST['id'])){
foreach($_POST['id'] as $id=>$value){
$sql = "UPDATE `i_table` SET `name` = '".$_POST['name'][$id]."', `rcv_qty` = '".$_POST['rcv'][$id]."', `rec_date` = '".$_POST['name'][$id]."' WHERE `id` = ".$id."";
mysql_query($sql,$con);
}
}
?>
note the foreach() is just an example. You will want to sanitize your data before inserting/updating to prevent sql injection. see How can I prevent SQL injection in PHP?
i make two web page,i want to get data from first page on second page,if user click edit message then move first page and show the entered data of user,here is my code:
first.php
<h1>Compose Message</h1>
<script type="text/javascript" src="<?=MURL?>/js/ckeditor/ckeditor.js"></script>
<script src="//ticket_inspector_new.com/js/tooltip.js" type="text/javascript"> </script>
<form action="" method="post" id="form" enctype="multipart/form-data">
<table class="form">
<tr class="heading">
<th style="width: 25%;">Recipients</th>
<th style="width: 75%;"> </th>
</tr>
<tr>
<td>
<label for="campaigns">Campaigns</label>
</td>
<td>
<select name="events[]" multiple size="10" >
<?
$select = sprintf ("SELECT event_id,event_name
FROM `events`
WHERE (`user_id` = '%s') order by event_name",
$GLOBALS ['mysqli']->real_escape_string ($_SESSION['user_id']));
$res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__);
if ($res->num_rows > 0)
{
while($row = $res->fetch_assoc ())
{
?>
<option value="<?=$row['event_id']?>"><?=$row['event_name']?></option>
<?
}
}
?>
</select>
</td>
</tr>
<tr>
<td style="padding-left:95px;">
<label for="fromdate">Registrants From</label>
</td>
<td style="padding-left:10px;">
<input name="fromdate" type="text" value="" class="calendar time" id="fromdate" size="30" />
<label for="todate">To</label>
<input name="todate" type="text" value="" class="calendar time" id="todate" size="30" />
</td>
</tr>
<tr>
<td>
<label for="upload">Upload CSV</label>
<span class="helptip">
Select CSV file for upload.
</span>
</td>
<td>
<input name="uploadcsv" type="file" />
</td>
</tr>
<tr class="heading">
<th style="width: 25%;">Message</th>
<th style="width: 75%;"> </th>
</tr>
<tr>
<td>
<label for="description">Description(optional)</label>
</td>
<td>
<input name="description" type="text" value="" id="description" size="35" />
</td>
</tr>
<tr>
<td>
<label for="subject">Subject</label>
</td>
<td>
<input name="subject" type="text" value="" id="subject" size="35" />
</td>
</tr>
<tr>
<td>
<label for="fromname">From Name</label>
</td>
<td>
<input name="fromname" type="text" value="" id="fromname" size="27" />
</td>
</tr>
<tr>
<td>
<label for="replyto">Reply To</label>
</td>
<td>
<input name="replyto" type="text" value="" id="replyto" size="27" />
</td>
</tr>
<tr>
<td>
<label for="senddatetime">Send Date/Time</label>
<span class="helptip">
Click the calender to select the date you wish ans select time zone from select box.
</span>
</td>
<td>
<input name="senddatetime" type="text" value="" class="calendar time" id="senddatetime" size="30" />
<select name="timezone" id="timezone">
<option value="Pacific/Honolulu">Hawaii-Aleutian Time (Honolulu, no DST)
</option><option value="America/Anchorage">Alaska Time (Anchorage)</option><option value="America/Los_Angeles"
selected="selected">Pacific Time (Los Angeles)</option><option value="America/Denver">Mountain Time (Denver)</option><option
value="America/Phoenix">Mountain Time (Phoenix, no DST)</option><option value="America/Chicago">Central Time (Chicago)
</option><option value="America/Regina">Central Time (Regina, no DST)</option><option value="America/New_York">Eastern Time
(New York)</option><option value="America/Halifax">Atlantic Time (Halifax)</option>
</select>
<script>
var list = document.getElementById('timezone');
var selval = "0";
for(var i = 0; i < list.options.length; ++i)
{
if(list.options[i].value==selval)
{
list.options[i].selected = true;
i=list.options.length;
}
}
</script>
</td>
</tr>
<tr>
<td>
<label for="message">Message</label>
</td>
<td colspan="2">
<textarea name="message" class="ckeditor" id="message" cols="90" rows="15" style="width: 100%;"></textarea>
</td>
</tr>
</table>
<table class="form">
<tr class="heading">
<th style="width:100%; background-color:#C4C4FE; font-size:10px; font-weight:normal;">Emails can take upto 30 minutes to Send.We have zero tolerance for spam messages.Every message sent out is reviewed for spam.Any spam messages sent will result in termination of account.</th>
</tr>
</table>
<p class="center_align">
<input type="submit" class="button arrow" name="submit_skip" value="Continue" />
</p>
</form>
and here is my second page:
<h1>Confirm Message</h1>
<?
$recepients=0;
$count=count($_POST['events']);
?>
<input type="button" class="button edit" name="submit_skip" value="Edit Message" />
<input type="submit" class="button email" name="submit_email" value="Send Message" />
i want when user click on edit massage it moves previous page and values shown in fields
?>
Since you want to send form to a two different scripts I suggest that you use 2 forms:
<form action="formfilling.php">
<?php
//prepare the recived POST to send back:
foreach( $_POST as $key => $value ){
if( is_array($_POST[$key]) ){ //if post is array e.g. your events[]
foreach( $_POST[$key] as $subvalue ){
echo '<input type="hidden"'
.' name="'.htmlspecialchars($key).'[]"'
.' value="'.htmlspecialchars($subvalue).'">'."\n";
}
} else{
echo '<input type="hidden"'
.' name="'.htmlspecialchars($key).'"'
.' value="'.htmlspecialchars($value).'">'."\n";
}
}
?>
<input type="submit" class="button edit" name="submit_skip" value="Edit Message" />
</form>
<form action="submitemail.php">
<?php //possibly show message? ?>
<input type="submit" class="button email" name="submit_email" value="Send Message" />
</form>
And then in formfilling check if input is not empty if not => echo its value
if( !empty($_POST['inputName']) ) echo htmlspecialchars($_POST['inputName']);
And for the array something like
if( !empty($_POST['inputName']) ){
foreach( $_POST['inputName'] as $val ){
//Test if the current option has the value of $val if so:
echo /*OPTION with SELECTED*/;
}
}
ALWAYS USE HTML ESCAPING when printing/echoing $_POST/$_GET
=> dont trust the users!
=> in PHP there is a htmlspecialchars() function
go to the previous page with javascript
window.history.go(-1)