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
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.
Question.php
<?php
include 'Pre-function.php'
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="CSS/Start.css">
</head>
<body>
<div class="nav">
Home
News
Contact
</div>
<div class="question">
<div class="A4">
<form action="Answer.php" method="post">
<?php getQuestion($conn); ?>
<input type="submit" name="Submit" value="Submit">
</form>
</div>
</div>
</body>
</html>
Its html page to ask question
Pre-function.php
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<h2 class="qtitle">'.$question_body.'</h2>
<label for='.$question_body.'>Yes</label>
<input type="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="'.$question_id.'" value="No">
<input type="hidden" name="option_b" value="'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
}
}
?>
Basically this form asked question whether yes or no using radio button. $option_a == 'Yes' and $option_b == 'No'. The question is like this "Are you have fever ?". So when i submit the value did not pass to the 'Answer.php' page.
'Answer.php' page.
<?php
include 'conn.php';
if(isset($_POST['Submit']) && !empty($_POST['Submit'])){
echo $_POST['option_a'];
echo 'succeed';
}
else{
echo 'no data';
}
?>
In this page have error undefined_index value but still echo succeed.
Your HTML code should look like the following:
<input type="radio" name="whatevername" value="Yes">
<input type="hidden" name="whatevername" value="No">
You can use PHP to insert whatever values you want but you need the radio button name to be the same.
Then if you want to echo that in PHP you'd use:
echo $_POST['whatevername']; //same name you used in the form
You are passing value as name for radio buttons
name="'.$option_a.'"
This will result you in name ="Yes"
And you are trying to fetch echo $_POST['option_a']; where option_a is not defined.
Try this
<input type="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
Same for other radio button
Try this one :
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
echo '<div class="A4">';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<div class="A4">
<h2 class="qtitle">'.$question_body.'</h2>
<form action="Answer.php" method="post">
<label for='.$question_body.'>Yes</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
echo'
<input type="submit" name="Submit" value="Submit">
</form>
</div>';
}
}
?>
<?php
include 'conn.php';
if(isset($_POST['submitted']) && !empty($_POST['submitted'])){
$questionAndOptions = $_POST['radioQuestions'];
foreach ($questionAndOptions as $questionAndOption) {
$arrQuestionAndOption = explode("-", $questionAndOption);
echo $arrQuestionAndOption[0]; //question
echo $arrQuestionAndOption[1]; //option
}
echo 'succeed';
}
else{
echo 'no data';
}
?>
<?php
$sel22 = mysql_query("SELECT id1nev, id2nev, id2, accept FROM barat WHERE id1= '$kariidje' AND accept = '1' ") or die("CANNOT FETCH DATA FOR ADMIN " . mysql_error());
if (mysql_num_rows($sel22) > 0) {
$i=0;
while ($data22 = mysql_fetch_array($sel22)) {
$i++;
?><tr>
<td>
<?php
include 'elemek/connection.php';
$idkitfogadel = $data22['id2'];
if(isset($_POST["submitelutasit"])){
$updatebaratt1 = "DELETE FROM barat WHERE id1 = $kariidje and id2 = $idkitfogadel";
mysql_query($updatebaratt1);
//header("Location: friends.php");
}
?>
<form id="form<?php echo $i; ?>" method="POST" action="#">
<Button type="SUBMIT" name="submitelutasit" id="submitelutasit<?php echo $i;?>" value="!"/></button>
</form>
</td>
<td align="center">
<a>Something</a>
</td>
</tr>
<?php
}
This is my code in my page. The problem is if I press the button in any line its delete all my data and not only what i want. Each line each button when I press a button its delete that line where is it. This is what i want. I cant solve this problem (sad).
Thank you
I will just assume that you are just playing around, and that's not even near to the production code.
Add input field to your form with id2 so you can use it later.
<form id="form<?php echo $i; ?>" method="POST" action="#">
<input type="hidden" name="id2" value="<?php echo $data22['id2'] ?>">
<Button type="SUBMIT" name="submitelutasit" id="submitelutasit<?php echo $i;?>" value="!"/></button>
</form>
And use it like this:
$idkitfogadel = $_POST['id2'];
Inside your if(isset($_POST["submitelutasit"])) statement.
I'm sure that little kitty died somewhere right now :(
I have a check box inside a while loop like this:
<form method="POST">
<?php $sql= mysql_query("SELECT * FROM names WHERE `id` ='$id' ");
while ($get = mysql_fetch_array($sql)){ ?>
<input type="checkbox" name="id_names" value="<? echo $get ['id'];?>"><?php echo $get ['name']; ?>
<?php } ?>
<input id="submitbtn" type="submit" value="Submit" /><br><br>
</form>
The problem is at this part I am unable to get specific checkbox properties and even if the user selects two check boxes I am unable to echo the id out
<?php
if(isset($_POST['id_names']))
{
$id_names= $_POST['id_names'];
$email = mysql_query("SELECT `email` FROM users WHERE `id` = '$id_names' ");
while ($getemail = mysql_fetch_array($email))
{
echo $getemail['email'];
}
}
?>
I have tried searching for answers but I am unable to understand them. Is there a simple way to do this?
The form name name="id_names" needs to be an array to allow the parameter to carry more than one value: name="id_names[]".
$_POST['id_names'] will now be an array of all the posted values.
Here your input field is multiple so you have to use name attribute as a array:
FYI: You are using mysql that is deprecated you should use mysqli/pdo.
<form method="POST" action="test.php">
<?php $sql= mysql_query("SELECT * FROM names WHERE `id` =$id ");
while ($get = mysql_fetch_array($sql)){ ?>
<input type="checkbox" name="id_names[]" value="<?php echo $get['id'];?>"><?php echo $get['name']; ?>
<input type="checkbox" name="id_names[]" value="<?php echo $get['id'];?>"><?php echo $get['name']; ?>
<?php } ?>
<input id="submitbtn" type="submit" value="Submit" /><br><br>
</form>
Form action: test.php (If your query is okay.)
<?php
if(isset($_POST['id_names'])){
foreach ($_POST['id_names'] as $id) {
$email = mysql_query("SELECT `email` FROM users WHERE `id` = $id");
$getemail = mysql_fetch_array($email); //Here always data will single so no need while loop
print_r($getemail);
}
}
?>
It's all going wrong. I need to output a form onto my website that will do 1 of 2 things:
If the user already has content in the database, provide a form that posts to self to update the existing content.
If the user does not have content in the database, provide a form to let the user add information to the database.
The forms should submit to themselves to keep coding tidy. I'm getting into a right mess. I'll show what I have so far, but I'm getting in a muddle.
//look in db to see if content exists, if it does set variable
$result = mysql_query(
"SELECT * from tbl_profiles
WHERE user_id = $who
");
while($row = mysql_fetch_array($result))
{
$profileText = $row['text'];
}
// Check if user has content in db
$result = mysql_query(
"SELECT * FROM tbl_profiles WHERE user_id='$who'");
if(mysql_fetch_array($result) !== false){
echo
'<form action="../edit/indexUpdate.php" method="post" name="edit">
Comments:<br />
<textarea name="updatedText" id="comments">' .
$profileText .'
</textarea><br />
<input type="submit" value="Submit" />
</form>'
;}
else{
$profileText = $row['text'];
echo
"<form action='../edit/index.php' method='post' name='add'>
Comments:<br />
<textarea name='comments' id='comments'>" .
$profileText
."</textarea><br />
<input type='submit' value='Submit' />
</form>"
;}?>
You've pretty much got the functionality there, just needs tidying up.
Try something like this:
<?php
//look in db to see if content exists, if it does set variable
$profileText="";
if($result = mysql_query("SELECT * from tbl_profiles WHERE user_id = $who")) {
while($row = mysql_fetch_array($result))
{
$profileText .= $row['text'];
}
?>
<form action="../edit/indexUpdate.php" method="post" name="edit">
Comments:<br />
<textarea name="updatedText" id="comments">
<?php echo $profileText; ?>
</textarea><br />
<input type="submit" value="Submit" />
</form>
<?php
} else {
?>
<form action='../edit/index.php' method='post' name='add'>
Comments:<br />
<textarea name='comments' id='comments'>
<?php echo $profileText; ?>
</textarea><br />
<input type='submit' value='Submit' />
</form>
<?php
}
?>
The basic idea is to add a record if new and update if not. What you can do is use an id to represent the record or -1 if it's a new entry
Something along the lines of:
//Defaults
$recordid=-1;
$name='';
$comments='';
//look in db to see if content exists, if it does set variable
$result = mysql_query(
"SELECT * from tbl_profiles
WHERE user_id = $who
");
// Check if user has content in db
$result = mysql_query(
"SELECT * FROM tbl_profiles WHERE user_id='$who'");
if(mysql_fetch_array($result) !== false){
//Yes. Get the id
$recordid = $result->id;
//Get the values
$name= $result->name;
$comments= $result->name;
}
<form action="../edit/index.php" method="post" name="formdata">
<input type="hidden" name="recordid" value="<? echo htmlspecialchars($recordid) ?>">
<input type="hidden" name="name" value="<? echo htmlspecialchars($name) ?>">
<textarea name="comments" id="comments"><? echo htmlspecialchars($comments) ?></textarea>
<input type="submit" value="submit"/>
</form>
This way a new form will have a -1 but an existing will have an id.
As an additional point it is very important to sanitize your inputs for SQL and what you output in HTML to stop SQL Injections. For your reference on this:
SQL
Little Bobby Tables
Cross Site Scripting