I have a SQL query statement that will return a particular set of result. Such as ID, Names, Price. I have no problem with that.
However i am trying to add a link within the echo loop and set ID as the value so that i can post it to another page. Would that be possible ?
while ($row = mysql_fetch_array($result)) {
echo
"".$row{'Url'}."<br>";
echo
"Name:".$row{'Name'}."<br>";
echo
"Price: $ ".$row{'Price'}."<br>";
echo
'<div class = "qwerty" data-countdown= '.$row{'Time'}.'></div>';
echo
"Location:".$row{'Location'}."<br>";
echo
"Description:".$row{'Description'}."<br>";
echo ''.$row{'ID'}.'';
echo 'Show Comments
<form id="displayComments" style="display:none" target="jsScript()" method="post">
<input type="hidden" name="run" value=".$row{'ID'}." />
</form>';
You missed quotes. value="'.$row['ID'].'"
echo 'Show Comments
<form id="displayComments" style="display:none" target="jsScript()" method="post">
<input type="hidden" name="run" value="'.$row['ID'].'" />
</form>';
And php array use [ ]
http://ua2.php.net/manual/en/language.types.array.php
Related
I'm trying to update the table in DB and every row (result) has its own update button which is a form. When i click the button nothing happens because I don't know how to transfer value of id to a form and then to a UPDATE query.
while(list($naziv,$tvrtka_id)=mysqli_fetch_row($resultA))
{
echo "<tr>";
echo "<td>".$naziv."</td>";
echo "<td>"?>
<form action="" method="POST">
<input type="hidden" name="id" value="<?php $tvrtka_id; ?>">
<input type="submit" name="odobriZahtjev" value="Odobri zahtjev">
</form> <?php "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
} //this is from a if statment which creates table
if(isset($_POST['odobriZahtjev']))
{
$firmId = $_POST['id'];
$updateAnswers = "UPDATE tvrtka
SET zahtjev = '0', preostaliOdgovori=preostaliOdgovori + 10
WHERE tvrtka.tvrtka_id='$firmId'";
$result=queryDB($connect,$updateAnswers);
}
When button is clicked the value of a request for answers is set to 0 and 10 answers are added to company.
tvrtka_id is the id which is supposed to go to a UPDATE query.
When using a PHP var inside HTML you have to print it to the HTML (using echo in your case).
<input type="hidden" name="id" value="<?php echo $tvrtka_id; ?>">
This will probably solve the problem with your update query when the form is submitted too
I want to add value of product_id inside database using bidupdate.php.
But everytime i try this. It says Error in connection as explained in die function in php. The value of product_id is not passing. I think there is some error in the value phase. Please check it.
<?php
if(isset($_SESSION['user_id'])){
echo '<form id="myForm" method="POST" action="bidupdate.php">';
echo '<input type="hidden" name="product_id" value = "<?php echo '.$_REQUEST['product_id'].'">';
echo '<button input="submit" name="bid">Bid Now</button>';
echo '</form>';
}
?>
You can't call the value in php close within php. that is possible with html. Use this code
<?php
if(isset($_SESSION['user_id'])){
echo '<form id="myForm" method="POST" action="bidupdate.php">';
echo '<input type="hidden" name="product_id" value = "'.$_REQUEST['product_id'].'">';
echo '<button input="submit" name="bid">Bid Now</button>';
echo '</form>';
}
?>
This is Wrong:
echo '<input type="hidden" name="product_id" value = "<?php echo '.$_REQUEST['product_id'].'">';
You cannot use <?php ?> inside PHP code.
Corrected code:
echo '<input type="hidden" name="product_id" value = "'.$_REQUEST['product_id'].'">';
I assume I have something stupid happening due to my lack of experience.
On a form on members.php I have:
$column = 0;
echo "<Form Name =member Method =POST ACTION = individual.php>";
echo "<table>";
echo "<tbody>";
while($row = $rs->fetch_assoc()) {
if ($column == 0) {
echo "<tr>";
}
echo '<td><INPUT TYPE = Submit NAME="'. $row['num'].'" VALUE ="'. $row['name'].'" id=Submit></td>';
$column++;
if ($column >= 5) {
echo "</tr>";
$row++;
$column=0;
}
}
echo "</tbody>";
echo "</table>";
echo "</form>";
On individual.php I have
print_r($_POST);
With the result being Array ( [16] => LUKE ) which is what is expected.
but when I try to
$name=$_POST['name'];
$num=$_POST['num'];
echo "<br>".$name." ".$num."<br>";
I do not get any results.
I mainly want to get $row['num'] but also did ['name'] to make sure I didn't transpose what I was trying to achieve.
I basically want to take what was selected on previous form and insert into a
select * from table where number=$num;
The name of your input fields are not num and name, instead, they are the values of these two keys in the array $row. That is why you are unable to retrieve these values on the backend.
Change your code to:
<input type="hidden" name="name" value="<?php echo $row['name']; ?>;">
<input type="hidden" name="num" value="<?php echo $row['num']; ?>;">
<input type="submit" value="Submit!" name="submit_form">
For obtain POST value for 'name' and 'num', you have to write:
<input type="text" name="name" value="<?php echo $row['name']; ?>">
<input type="text" name="num" value="<?php echo $row['num']; ?>">
type can be 'text' or 'hidden'
Enjoy your code!
If you want your form to return values that you can access using $_POST['name'] and $_POST['num'], you need to set name and num as input names on your form, and the appropriate values as the input values. If you don't want to add them as visible/editable values in your form, the usual way to do it is to use inputs of type hidden:
<form action="individual.php">
<input type="hidden" name="num" value="16" />
<input type="hidden" name="name" value="LUKE" />
<input type="submit" />
</form>
As it is, $_POST does contain the values that you're want, but they're not in an accessible format, because you've set 16 as an input name, so it turns into a key in the $_POST associative array. The form above will set num and name as keys in $_POST, and 16 and LUKE respectively as the values.
so i have this code fragment here..
if($numTF > 0)
{
echo "TRUE-AND-FALSE QUESTIONS: Enter them below followed by their correct answer.";
echo "<br>";?>
<form method="post" action="" name="quizform">
<?php for ($i=1; $i<=$numTF; $i++)
{
echo "Question"." ".$i;
?>`
<p><textarea name='question<?php echo $i; ?>' rows=3 cols=90></textarea></p>
<input type="radio" name="answer<?php echo $i; ?>" value="True"> True
<input type='radio' name="answer<?php echo $i; ?>" value="False"> False<br><br><br>
<?php
}
}
... i am making a quiz maker in php...
the first thing to do is to set up the desired number of questions, so the value entered will go on the $numTF variable. Depending on the entered value, the textarea part will be printed. and there will be different names for each text area. AND THE CODE ABOVE IS WHERE U PRINT THE FORMS AFTER U ENTER THE DESIRED VALUE.
The next thing is to save that in a database. since the name of each textarea will be based on a variable value($i) that is used in a loop (name="answer") , HOW CAN I USE IT IN $_POST??? Like, would i do it like this?? ($_POST['question']).
HOW CAN I SAVE THESE QUESTIONS IN A DATABASE??
PLEASE HELP ME ....
I WOULD BE SO MUCH MUCH MUCH GRATEFUL FOR A LIL HELP.
<?
var_dump($_POST);
?>
<form method="post">
<?
$numTF=4;
if($numTF > 0)
{
echo "TRUE-AND-FALSE QUESTIONS: Enter them below followed by their correct answer.";
echo "<br>";?>
<form method="post" action="" name="quizform">
<?php for ($i=1; $i<=$numTF; $i++)
{
echo "Question"." ".$i;
?>`
<p><textarea name='question[<?php echo $i; ?>]' rows=3 cols=90></textarea></p>
<input type="radio" name="answer[<?php echo $i; ?>]" value="True"> True
<input type='radio' name="answer[<?php echo $i; ?>]" value="False"> False<br><br><br>
<?php
}
}
?>
<input type="submit" name="submit" value="submit"/>
</form>
Use $_POST['question'][1] // To get first question
Use $_POST['answer'][1] // To get first answer
Use loop to get all question and answers
I agree with Sachin as far as using name='question[]'. To answer question a little more as far as storing it in a database. Personally I would use a JSON array.
$store_answers = json_encode($_POST['answer']);
$store_questions = json_encode($_POST['question']);
Then just store $store_string in a TEXT field in your database. Then when you pull it back out of the database you can simple use:
$answers = json_decode($store_answers);
$questions = json_decode($store_questions);
Then you can loop through using a foreach like so:
foreach($questions as $key=>$question) {
echo "Question $key = {$answers[$key]} <br />";
}
This will display the results for each question.
I am POSTING two things. The comment, which works ok, but the second item I need to post is the $list['id'] that is unique to this each row. How do I include this unique id, when the user clicks POST so that it can be used on the page that it is being posted to.
foreach ($posts as $key => $list){
echo " <tr valign='top'>\n";
echo " <tr>$list['id']
<div class='comment_text'>
<form method='post' action='add_comment.php'>
<textarea name='comment'</textarea>
<input class='btn' type='submit' value='Post'/>
</form>
</div>
</td>\n";
echo "</tr>\n";
}
The page I am posting to looks like this:
<?php
$commenter_user_id = $_SESSION['user_id'];
$body = substr($_POST['comment'],0,400);
$post_id=;
add_comment($commenter_user_id,$post_id,$body);
$_SESSION['message'] = "Your comment has been added!";
header("Location:/social_learning/site_pages/profile.php");
?>
You can use hidden input:
<input type="hidden" name="postName" value="<?= $list['id'] ?>" />
Then in your PHP it's available in $_POST['postName'] (in accordance to the name attribute of the hidden input)