Hi I have a question about generating rows and columns. would like to ask about how can I make it in one page only. This is what I have tried.
HTML:
<html>
<head>
<title>Table Generator</title>
<body>
<center><h1>Generate Your Table</h1></center>
<div id="div1">
<center><h4>Enter number of Row and Column</h4>
<form action="get_table/execute_table.php" method="POST">
<label for="title">Row</label>
<input type="text" name="title1" placeholder="Row">
<br>
<label for="title">Column</label>
<input type="text" name="title2" placeholder="Column">
<br>
<input type="submit" name="submit" value="Generate Table"> </center>
</form>
</div>
</body>
PHP:
<?php
$row = $_POST['title1'];
$column = $_POST['title2'];
echo "<table border='1'>";
for($tr=1;$tr<=$row;$tr++){
echo "<tr>";
for($td=1;$td<=$column;$td++){
echo "<td>row: ".$tr." column: ".$td."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Yes, it is fully running but I want it in 1 page only. Thanks.
Normally, if you want it on the same page, you just omit the action="" with its value.
Then of course, put the php process in the same page as the form:
<div id="div1">
<center><h4>Enter number of Row and Column</h4>
<form action="" method="POST">
<!-- ^^ no more value, well you could just put the same filename -->
<label for="title">Row</label>
<input type="text" name="title1" placeholder="Row">
<br>
<label for="title">Column</label>
<input type="text" name="title2" placeholder="Column">
<br>
<input type="submit" name="submit" value="Generate Table"> </center>
</form>
</div>
<?php
$out = ''; // initialize a string holder and when the submission is done, concatenate all the strings
if(isset($_POST['submit'])) { // catch submission button
$row = $_POST['title1'];
$column = $_POST['title2'];
$out .= "<table border='1'>";
for($tr=1;$tr<=$row;$tr++){
$out .= "<tr>";
for($td=1;$td<=$column;$td++){
$out .= "<td>row: ".$tr." column: ".$td."</td>";
}
$out .= "</tr>";
}
$out .= "</table>";
}
echo $out; // finally echo it
to achieve this by using only one page just leave the action attribute empty. and add your php block on top.and make sure to save it as .php and place your php code block on top of all html
so in the end it will look like this
<?php
$row = $_POST['title1'];
$column = $_POST['title2'];
echo "<table border='1'>";
for($tr=1;$tr<=$row;$tr++){
echo "<tr>";
for($td=1;$td<=$column;$td++){
echo "<td>row: ".$tr." column: ".$td."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
<html>
<head>
<title>Table Generator</title>
<body>
<center><h1>Generate Your Table</h1></center>
<div id="div1">
<center><h4>Enter number of Row and Column</h4>
<form action="" method="POST">
<label for="title">Row</label>
<input type="text" name="title1" placeholder="Row">
<br>
<label for="title">Column</label>
<input type="text" name="title2" placeholder="Column">
<br>
<input type="submit" name="submit" value="Generate Table"> </center>
</form>
</div>
</body>
You need to post to the same page, and check if the PhP Post array has data yet.
Replace the form action with:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
Note: although most (all?) browsers support self-posts with a blank action, this is not technically W3C compliant.
Then when the page is submitted it will reload the same page and populate the POST array. Somewhere on your page add a condition similar to:
if($_POST['title']){
//do whatever get_table/execute_table.php did
}else{
//echo the form here or, if you're allowed, use an include()
}
More info on self-posting:
How do I make a PHP form that submits to self?
Related
I have two php files insert.php and function.php
In insert.php
<form action="<?php echo htmlspecialchars( $_SERVER['PHP_SELF'] ); ?>"
method="post">
<input type="hidden" name="Entry" value="<?php print $data; ?>"/>
<hr>
<h3>Entry your new entry</h3>
<?php
include('function.php');
$query_start = "SHOW COLUMNS FROM $data";
$result = mysqli_query( $conn, $query_start );
while($row = mysqli_fetch_array($result)){?>
<label for="<?php echo "$row[0]";?>"><?php display_text($row[0]);?></label>
<br>
<?php display_value($row[0]);
} ?>
<input class="btn btn-primary" type="submit" name="add" value="Add Entry">
</form>
And in function.php
<?php
function display_value($row){?>
<input class='form-control' type='text' placeholder='".$row."' name='".$row."'>
}?>
But I am not getting the form on the web page.
firstly you need to add a <form> tag
<?php
include('function.php');
$query_start = "SHOW COLUMNS FROM $data";
$result = mysqli_query( $conn, $query_start );
while($row = mysqli_fetch_array($result)){
$data = display_value($row[0]);
echo $data;
}
?>
you're just calling that function, for printing the values inside the function, you need to return data from function and store or print the data from you're calling.
$data = display_value($row[0]);
here variable $data will store the output, also you need to return the result from your function
<?php
function display_value($row) {
$data = '<input class="form-control" type="text" placeholder="'. $row .'" name="'. $row .'"><br /><br />';
return $data; //this return will be printed on another page
} ?>
I guess with "form" he meant the fields not the form tag.
#Rohit Sahu answer is wrong.. you dont need to pass the output with a return. Your way of doing it is not a beautiful way. But it is even working this way.
Are you sure your Mysql Query give results?
Example Code:
<?php
// functions.php
function display_value($row) { ?>
<input class='form-control' type="text" placeholder="<?php echo $row;?>" name="<?php echo "$row";?>"><br><br>
<?php } ?>
// insert.php
<form action="....">
<?php
$arr = array(1,2,3,4);
foreach($arr as $row) {
display_value($row);
}?>
</form>
Output:
<form action="....">
<input class='form-control' type="text" placeholder="1" name="1"><br><br>
<input class='form-control' type="text" placeholder="2" name="2"><br><br>
<input class='form-control' type="text" placeholder="3" name="3"><br><br>
<input class='form-control' type="text" placeholder="4" name="4"><br><br>
</form>
Result: It is working fine. You should better post all code to understand where is your mistake. It could be some whitespaces in files, it could be no results from MYSQL.. there are many options. It is difficult to say this way.
I have a form that is being created dynamically
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{ ?>
<input type="text" placeholder="<? echo $row['heading']?>" name="<? echo $row['heading']?>" />
<?}?>
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button>
</form>
But i a not able to understand how to carry the value of input to the addtable.php page
Can anyone please tell how to submit these values from this form
In your addtable.php, you need to print_r $_POST and see what inputs you're getting.
// addtable.php
echo "<pre>";
print_r($_POST);
print_r($_FILES); // If you're expecting a file input
Also, you should modify your HTML a little:
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0) {
while($row = mysqli_fetch_assoc($result)) { ?>
<input type="text" placeholder="<? echo $row['heading']?>" name="<? echo $row['heading']?>" />
<? } ?>
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button> <!--Place submit button outside loop -->
<?php } ?>
I think there is issue in submit button, you looping submit button with same name, try to put submit button out of while loop.
One of the reasons your PHP is not functioning correctly is because your PHP block, is not placed inside of PHP tags.
Your code should look like this:
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php // added the PHP tags
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
?> // escaped the php tag
<input type="text" placeholder="<?= $row['heading']; ?>" name="<?= $row['heading']; ?>" />
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button>
<?php
}
}
?>
Notice how I also escaped PHP when I wanted to display any HTML. Of course, you could simply echo these out, but you can do this too! Personal preference, really.
Another little alteration I made was, rather than using <?php echo ... ?> you can simply use <?= $row['...']; ?>. It is faster and cleaner too!
In your addtable.php:
<?php
$inputValue = $_POST['input_name'];
//the $inputValue is the input value that you posted from form.
echo $inputValue;
?>
To receive data in addtable.php you have to get them by name property of inputs in the form. So you have to define the names of inputs or to retrieve them again from db in addtable.php
try this ,
addtable.php
<?php
foreach ($_POST as $key => $value){
echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
}
?>
i hope it will be helpful.
You can create array of heading and access it in php:
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<input type="text" placeholder="<?php echo $row['heading']; ?>" name="heading[]"> />
<?
}
?>
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button>
<?php
}
?>
</form>
In addtable.php:
$headingArray = $_POST['heading']; // here you will get all the heading in array format
So I'm currently working on a little project that allows a user to input a number into a textbox, after clicking a button that says "add" it should store that value into an array and then allow the user to input another value into that array. There is also a button on the page when the user is finished and wants to sum the values called "Submit". The problem I'm running into is everytime the form posts back, it recreates a new blank array. Any tips?
See the code below:
<html>
<head>
</head>
<body>
<h2>Please Select your title and name:</h2>
<form action='<?php echo $_SERVER["PHP_SELF"]; ?>' method='post'>
<p>
<label for="strFirstname">Type number to add: </label>
<input type='text' name='strNumber' id='strNumber'/>
</p>
<p>
<input type='submit' name='submit' value='Add' />
</p>
<p>
<input type='submit' name='calculate' value='Compute' />
</p>
<?php
$array = array();
if (isset($_POST['submit']))
$num = $_POST['strNumber'];
$array[] = $num;
foreach($array as $num)
echo $num . ' + ';
if(isset($_POST['calculate']))
foreach($array as $num)
echo $num . ' + ';
?>
</form>
</body>
</html>
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<h2>Please Select your title and name:</h2>
<form action='' method='post'>
<p>
<label for="strFirstname">Type number to add: </label>
<input type='text' name='strNumber' id='strNumber'/>
</p>
<p>
<input type='submit' name='submit' value='Add' />
</p>
<p>
<input type='submit' name='calculate' value='Compute' />
<input type='submit' name='clear' value='clear' />
</p>
<?php
if (isset($_POST['submit'])) {
if(!array_key_exists("numbers", $_SESSION)) {
$_SESSION["numbers"] = array();
}
array_push($_SESSION["numbers"], $_POST["strNumber"]);
}
if(isset($_POST['clear'])) {
$_SESSION["numbers"] = array();
}
if(array_key_exists("numbers", $_SESSION)) {
echo implode("+", $_SESSION["numbers"]);
}
if(isset($_POST['calculate'])) {
if(array_key_exists("numbers", $_SESSION)) {
$expression = implode("+", $_SESSION["numbers"]);
eval( '$result = (' . $expression . ');' );
echo "=" . $result;
}
}
?>
</form>
</body>
</html>
Start a session
When the action is "submit"
Check if the session which will store the numbers is initialized
If it's not initialize it as an array
Finally push the number into the array
Check if there is a session initialized if there is print all the numbers ( you can use implode to do that)
if the action is calculate .. just make the calculation ( check eval function )
i wrote a piece of code to do multiple row deletions based on ticked check boxes.
Now i need to mordify it to do multiple row updates.
i have been failing to access the textbox values for each row.
here's my code, in general.
<?php
if (isset($_POST["SaveComments"]) && isset($_POST["SaveEachComment"]))
{
while(list($key, $val) = each($_POST["SaveEachComment"]))
{
if($val == 'commentbox')
{
// do the update here. $val contains the ID
}
}
}
?>
<form id="form1" name="form1" method="post" action="test.php">
<input type="checkbox" <?php echo 'name="SaveEachComment['.$row["comment"].']" ';?> value="commentbox" id="commentbox" />
<input type="text" name="rowcomment" size="55" value="<?php echo $comment;?>" />
<input type="submit" name="SaveComments" value="submit" />
</form>
I just added a for loop to print multiple form fields. Obviously your iteration would be as many as number of rows, so you can change that part of the code. But try this:
<?php
if (isset($_POST["SaveComments"]) && isset($_POST["SaveEachComment"]))
{
while(list($key, $val) = each($_POST["SaveEachComment"]))
{
if($val == 'commentbox')
{
echo $_POST['rowcomment'][$key] . "<br />\n";
// do the update here. $val contains the ID
}
}
}
?>
<form id="form1" name="form1" method="post" action="test.php">
<?php for ($i=0; $i<11; $i++) { ?>
<input type="checkbox" <?php echo 'name="SaveEachComment['.$i.']" ';?> value="commentbox" id="commentbox" />
<input type="text" name="rowcomment[<? echo $i?>]" size="55" value="<?php echo $comment;?>" />
<br />
<?php } ?>
<input type="submit" name="SaveComments" value="submit" />
</form>
Can you tell me why when I click call my data is not posting to my action?
<body>
<?php
$login = '1236567';
$password = '10152930';
$officeNumber = array('0212177899','027899899','09111');
?>
<div id="wrapper">
<form method="POST" action="https://live.domain.co.nz/call.php?login=<?php echo $login; ?>&password=<?php echo $password; ?>&aparty=<?php echo $number; ?>phone&bparty=<?php echo $number;?>">
<?php
echo '<label for="officeNumbers">Office Number: </label>';
echo '<select name="officeNumbers">';
foreach($officeNumber as $number)
{
echo '<option value="'.$number.'">'.$number.'</option>';
}
echo '</select>';
?>
<label for="callTo">Call: </label><input type="text" id="callTo">
<input type="submit" value="Call">
</form>
</div>
</body>
</html>
Your text input:
<input type="text" id="callTo">
… has no name attribute, so it cannot be a successful control (and thus submit any data).
Give it a name attribute.