how to send value to the from action page from database - php

I am creating a faq panel for there can be multiple answers for question and i want to take the answer id .because i am storing comment by answer id
the problem is that how to sent the $answer_id to the comment_submit_process.php and how to recognize the answer ?
$selected_ques= mysql_prep($_GET['ques']);
$query = "SELECT * FROM formanswer where question_id = {$selected_ques}";
$ans= mysql_query($query);
if($ans){
while($answer = mysql_fetch_array($ans))
//here is the form
<form id="add-comment" action="comment_submit_process.php" >
<textarea class="comment-submit-textarea" cols="78" name="comment" style="height: 64px;"></textarea>
<input type="submit" name="submitbutton" value="Add Comment" class="comment-submit-button" >
<br> <?php
$ans_id= $answer['id']; //i am fatching the $answer['id'] from database
?>
<input type="hidden" name="ques" value="<?php echo $_GET['$ans_id'] ?>" />
<span class="counter ">enter at least 15 characters</span>
<span class="form-error"></span>
</form>
<?php }} ?>

You might have typo here !! it should be..
<input type="hidden" name="ques" value="<?php echo $ans_id; ?>" />
Other thing, you can add get param to action link it self.
<form id="add-comment" action="comment_submit_process.php?<?php echo $answer['id']; ?>" >

Instead of setting the ans_id, every time to the hidden field .
Generate a string of ans_id seperated with "," until while loop ends append the string and assign that value to the hidden field and in form action page you can get that value and generate van array from that string with delimiter ",".Now you can have the array of ans_id in your form action page
$answer_array = "nothing";
while($answer = mysql_fetch_array($ans))
{
if( $answer_array == "nothing")
$answer_array = $answer;
else
$answer_array .= ",".$answer;
}
<input type="hidden" name="answer_arr" value="<?=$answer_array?>">
In Form action page you can get that hidden value
$ans_array= explode(",",$_GET['answer_arr']);

You can echo answer_id in form action tag as additional parameter like this:
<form id="add-comment" action="comment_submit_process.php?ans_id=$ans_id" >
//Your stuff here
</form>
in comment_submit_process.php you can identify answer by
$ans_id=$_GET['ans_id'];
You can do further processing by using $ans_id
Edit:
change this line:
<input type="hidden" name="ques" value="<?php echo $_GET['$ans_id'] ?>"
to:
<input type="hidden" name="ques" value="<?php echo $ans_id; ?>" />
so that value of that field would be $ans_id fetched from DB.

Related

Passing input values using post

I am trying to pass the value of <input id="ids" type="hidden" name="ids" value="<?php echo $key['product_ID'];?>"> to the exchange.php page. Lets say i have apple | 1 , orange | 2, pineapple | 3. However, whenever i try to submit the values to another page, I am getting the 3 as the echo value of $product. When i try to remove the type="hidden", I get the correct value but when I try to submit, it turns out to be a different value.
<form action="exchange.php" method="post">
<div class="row">
<?php $query="SELECT * FROM Product" ; $data=$ MySQLi_CON->query($query);
foreach ($data as $key ) { ?>
<strong>Name: </strong>
<?php echo $productname=$ key[ 'product_Name'];?>
<input id="ids" type="hidden" name="ids" value="<?php echo $key['product_ID'];?>">
<strong>Status: </strong>
<strong>Action: </strong>
<input type="submit" value="Exchange" name="exchange_submit" class="btn btn-info btn-xs">
<input id="id" name="id" type="hidden" value="<?php echo $id; ?>">
<?php } ?>
</div>
</form>
exchange.php
<?php
$id = $_POST['id'];
$product = $_POST['ids'];
echo $id;
echo $product;
exit;
?>
<?php echo $productname=$ key[ 'product_Name'];?>
//is it working not showing error
Whenever you execute a foreach loop, it will store the last output of the Database table in the hidden input of yours, and that's why no matter any input you give, it will take 3 as the id of the product.Try using a tag also.
You are using foreach for products so I consider you've more than one products.
but when you doing this you are overwriting the $_POST['ids'].
<input id="ids" type="hidden" name="ids" value="<?php echo $key['product_ID'];?>">
thats why its showing last product_ID present in table.

php form save input [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need your help with some form/php code:
2Fields first for name 2nd for fam.name
If the "User" fill both fields its echo what he filled in
If the "User" dont fill any field he get form back with an error message
Same if he just fill only 1 of the 2 fields
My question is if the "User" just fill 1 field and send it (submit) i want that he gets the error message like now but also save his input (the date he filled in the field) so he only has to fill the other field and not both again.
I tried it allready with session_start(); and stuff but failed.
<?php
$errormessage="";
//check if 1 of 2 fields is empty
if(empty($_POST['vorname'])){
$errormessage=$errormessage." First field ,";
}
if(empty($_POST['nachname'])){
$errormessage=$errormessage." Second field ,";
}
//Submit starts
if (isset($_POST['go'])) {
if (!empty($_POST['vorname'])&& !empty($_POST['nachname'])){
echo "First field : ".$_POST["vorname"]." ";
echo " 2nd field: ".$_POST["nachname"];
}//end empty
//ELse if inputs empty
if (!empty($errormessage)) { ?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="vorname" />
<input type="text" name="nachname" />
<input type="submit" name="go" />
<?php echo "error: ".$errormessage; ?>
</form>
<?php } //ende if errormessage
}//ende if isset go
else {//start side ?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="vorname" />
<input type="text" name="nachname" />
<input type="submit" name="go" />
</form>
<?php} //end else?>
If user specified some info only for one field and send it, why don't you paste correct filled data from $_POST var into value attribute of one of yours inputs:
<input type="text" name="vorname" value="<?php echo $correct_data ?>"/>
and show additional error message, like you do?
I mean, you can save old $_POST['vorname'] and $_POST['nachname'] into two extra vars, if one of the $_POST fields is empty, one of this var is empty, then add this vars in value attrubutes of your inputs in html form:
$errormessage = "";
$old_vorname = empty($_POST['vorname']) ? "" : $_POST['vorname'];
$old_nachname = empty($_POST['nachname']) ? "" : $_POST['nachname'];
....
//ELse if inputs empty
if (!empty($errormessage)) { ?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="vorname" value="<?php echo $old_vorname; ?>"/>
<input type="text" name="nachname" value="<?php echo $old_nachname; ?>"/>
<input type="submit" name="go" />
<?php echo "error: ".$errormessage; ?>
</form>
Value attribute of html input element contains it actual content, you can see on webpage or edit.

Edit Mysql Table With A Form

I want to edit mysql data using a contact form. When the user clicks edit on the specific post they want to change, it takes them to a form that has the post values in the input fields, and they can change whatever they want about the post. I already have this part working but now I need the change to actually take effect once the user clicks submit. How can I accomplish this?
Here is the PHP that displays all the text from the blog post in mysql into the input fields when the user clicks edit.
<?php
include "../php/db_connect.php";
if (isset($_GET['id'])) {
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM articles WHERE id='$id'");
while($row = mysql_fetch_array($sql)) {
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$written_by = $row['written_by'];
$category = $row['category'];
}
?>
<div class="content_wrapper">
<h1 class="content_heading">Edit A Blog Post</h1>
<form method="post" action="php/edit_article_process.php" enctype="multipart/form-data" id="upload_article_form">
<span>Title</span>
<input type="text" name="title" class="input" value="<?php echo $title; ?>"/>
<span>Article</span>
<textarea rows="20" name="ckeditor1" class="input" style="resize:none;"><?php echo $content ?></textarea>
<span>Author</span>
<input type="text" name="written_by" class="input short_input" value="<?php echo $written_by; ?>"/>
<span>Category - <i>Spell it right nigga</i></li></span>
<input type="text" name="category" class="input short_input" value="<?php echo $category?>"/>
<input type="submit" name="update" class="input submit_button"/>
</form>
<?php } ?>
</div>
Now all I need is for the change to take effect when the user clicks submit.
Your form attribute method has a value of post which is not the same method you used inside the isset() function
You should use:
$_GET['id'] //if your method="get"
and
$_POST['id'] //if your method="post"
if you want the change to appear directly you should re-fresh the page after editing the values
header('Location: www.samepage.php');

HTML Form $_POST does not work

menus.php
<?php
$con=mysqli_connect("localhost","root","","mmogezgini");
$menuler = mysqli_query($con,"SELECT * FROM menuler");
while($menu = mysqli_fetch_array($menuler)) {
$isim = $menu["isim"];
$url = $menu["url"];
?>
<form method="post">
Menü İsmi :<input name="menu_isim" value="<?php echo $isim; ?>" disabled> |
Menü URL : <input name="menu_url" value="<?php echo $url;?>"disabled>
<button type="submit" formmethod="post" formaction="menu_duzenle.php">Düzenle</button>
<button formaction="menu_sil.php">Sil</button>
</form>
<br>
<?php
}
?>
edit_menu.php
<form method="post" action="menu_duzenle_islem.php">
Şuanki İsim : <input name="menu_isim" value="<?php echo $_POST['menu_isim'] ?>"disabled>
Yeni İsim : <input name="yeni_menu_isim" placeholder="yeni menü ismini giriniz.">
</form>
My Problem is form in menus.php wont send $_POST['menu_isim'] to *edit_menu.php*.need more to write for details
Disabled inputs are not considered "valid" for being passed through a form.
To make an input read-only, use the readonly attribute instead.
Keep in mind, however, that this kind of thing CANNOT be trusted. You should save the value in a $_SESSION variable and retrieve it from there.
Remove the disabled attribute. If you want the user not to edit the input use readonly attribute.

Change hidden field value on Submit click

I have a form with multiple items and an id attributed to each of these items, on Submit I want to be able to grab the id of the item that was clicked - I have tried using js, something like:
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>" type="submit" value="Add">
</form>
I want to be able to grab this value: $item_id = $_POST["item_id"]; on add_item_cart.php, but this doesn't seem to be working.
Is this a problem with my js syntax or is my logic not plausible to solve this problem? Is it submitting before changing the value?
EDIT:
Let's see if I can explain myself better, I want to assign that hidden value dynamically, imagine that my form has 3 submit buttons (one for each item displayed). Depending on the one that is clicked, I want to pass the item's id to my hidden field, so if I click button1 - $_POST["item_id"]=1, button2 - $_POST["item_id"]=2... etc
Here is my actual form (non simplified example)
<form method="post" action="add_item_cart.php">
<table style="width:600px">
<tr>
<?php foreach ($items as $item): ?>
<td>
<center>
<span style="font-size:20px"><?php echo $item["item_name"] ?></span><br/>
€<?php echo $item["price"] ?><br/>
Quantidade: <input type="text" value="1" style="width:30px"><br/>
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo $item["id"]; ?>" type="submit" value="Adicionar">
</center>
</td>
<?php endforeach; ?>
</tr>
</table>
</form>
When your form is posted and you want to collect the item_id value on add_item_cart.php first you need to actually assign a value to id as in (Assuming item_id is a php variable). The id is just used for setting the css editing not a value...
<input type="hidden" id="item_id" value="<?php echo $item_id; ?>" name="item_id">
you cannot have id='' for the value because 'value' is value.
Then you can get that value on your other page with:
<?php
if(isset($_POST['item_id'])){
$item_id = $_POST['item_id'];
}
?>
If you want to edit the variable after post depending on which button you hit you can try.
<input name="submit_item1" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item2" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item3" id="btn_sub" name="button1" type="submit" value="Add">
Then on the top of your page you can do.
<?php
if(isset($_POST['submit_item1'])){
$item_id = 1;
}
if(isset($_POST['submit_item2'])){
$item_id = 2;
}
if(isset($_POST['submit_item3'])){
$item_id = 3;
}
?>
If you are creating forms from an array of items you could do something like, (assuming you somehow have an id associated with those items; I would need to know more information about how you are making your item list. But it would generally do like this.
<?php
foreach($item_array as $item){
?>
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id" value="<?php echo $item['id']; ?>">
<input name="submit_item" id="btn_sub" type="submit" value="Add">
</form>
<?php
}
?>
Then on the top of your page you can just get $_POST['item_id'] and since that value is dynamically set you do not need any conditionals you can just get that value and run any query.
UPDATE
Use normal button instead of submit button and use javascript for submitting the form.
<form method="post" name="f1" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>; document.f1.submit();" type="button" value="Add">

Categories