HTML Form $_POST does not work - php

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.

Related

PHP isset not accepting form name

I am having an issue trying to get my form with a hidden input field to be recognized when I check if it isset. Not sure if it is because I am running the form in a loop or what. I do know when I click submit for any of the fields from the loop the value of the hidden field is registering properly, it is just somehow the 'form1' is not registering as isset. Any ideas?
<?php
if(isset($_POST['form1']))
{
echo $_POST['cid'];
} else {echo "nope!";}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" name="form1">
// gets array
$listchars = $user->getCharacterList($_SESSION['user_id']);
// loops through array
foreach($listchars as $chardata){
echo $chardata['name']; ?>
<input type="hidden" name="cid" value="<?php echo $chardata['cid'];?>">
<input type="submit" name="submit" value="submit">
<?php } ?>
</form>
The form name is not submitted
Use
<input type="submit" name="form_name">
or
<input type="hidden" name="form_name">

retrieve users input with PHP and echo it

I was wondering if it was possible to take HTML user input using PHP (preferably the ID or something I can use numbers in) and to save confusion just echo it back.
So I have some example code here:
<input type="number" maxlength="3" name="test" id="1">
<input type="number" maxlength="3" name="test" id="2">
<input type="number" maxlength="3" name="test" id="2">
What I was looking for is a way where I could use their input and well.... echo it back for now.
if you already know how to submit a form you can use php on the other side like this to echo it out
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
<?php
if (isset($_POST['name'])){
$userinput = $_POST['name'];
echo $userinput;}
?>
in your example all three inputs are named "test" so youll need a different name for each one. My example above uses the "name" of the input to capture it. If your using "GET" change my $_POST['name'] to $_GET['name']
Which method did you use for these inputs? Name them differently, then retrieve the data with:
<?php echo $_GET['test1']; ?>
<?php echo $_GET['test2']; ?>
<?php echo $_GET['test3']; ?>
If you used POST type in the input method, then switch for:
<?php echo $_POST['test1']; ?>
<?php echo $_POST['test2']; ?>
<?php echo $_post['test3']; ?>

Update form fields after posting with PHP_SELF

I am using php_self to submit a form. Once the data has been posted, I want to pass a calculated value to another form field on the same page, original form.
The $title_insurance field stays blank. Any ideas on why? Thanks!
<?php
if(isset($_POST['submit']))
{
$sale_price = $_POST['sale_price']; // posted value
$title_insurance = ($sale_price * 0.00575) + 200;
?>
<script type="text/javascript">
document.getElementById("title_insurance").value='<?php echo $title_insurance ; ?>';
</script>
<?php } ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input name="sale_price" type="text" id="sale_price" size="15">
<input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
<input name="submit" type="submit" class="bordered" id="submit" value="Calculate" />
</form>
The submit button is called button, also if you are outputting a javascript to amend the value it need to be run after the DOM has created the element title_insurance.
if(isset($_POST['button']))
{
$sale_price = $_POST['sale_price']; // posted value
$title_insurance = ($sale_price * 0.00575) + 200;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input name="sale_price" type="text" id="sale_price" size="15">
<input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
<input name="button" type="submit" class="bordered" id="button" value="Calculate" />
</form>
<script type="text/javascript">
document.getElementById("title_insurance").value='<?php echo $title_insurance ; ?>';
</script>
A better way in this case would be to forget about the javascript as it is unnecessary and do this
// I am assuming you have initialized $title_insurance
// somewhere above here to its default value!!!!
$title_insurance = isset($_POST['button']) ? ($_POST['sale_price'] * 0.00575) + 200 : $title_insurance;
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input name="sale_price" type="text" id="sale_price" size="15">
<input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
<input name="button" type="submit" class="bordered" id="button" value="Calculate" />
</form>
You have an extra space in your getElementById parameter:
// VV
document.getElementById("title_insurance ").value='<?php echo $title_insurance ; ?>';
What you want to do is best done by AJAX. The <form> construction is outdated and not useful unless you are transferring the user to another page and sending some data along with it - or, if you are finished getting user data and just want to process what was entered and display a completion message.
If you wish to continue processing on the same page, then AJAX is the way to go. And the best way to use AJAX is to have a separate processor file (PHP) that receives the data, processes it, and sends it back.
To convert a <form> construct to AJAX, you really just need to remove the <form></form> tags and convert the submit button from type="submit" to type="button" id="mybutton", and use the IDs on the button and on the other elements to grab the data they contain and feed them to the AJAX code block. The examples in the link at bottom shows what you need to know - they are simple, helpful examples.
To conserve resources, you can use the same PHP processor page for multiple AJAX requests -- just send a variable (eg. 'request=save_to_db&first_name=bob&last_name=jones', ) and test for what "request" is received, that will determine what your PHP processor file does and echoes back.
This post, and the examples it contains, will help.
try this first
In you coding you missed this $_POST['button']
and
<?php
if(isset($_POST['button']))
{
$sale_price = $_POST['sale_price']; // posted value
$title_insurance = ($sale_price * 0.00575) + 200;
?>
<script type="text/javascript">
document.getElementById("title_insurance ").value='<?php echo $title_insurance ; ?>';
</script>
<?php } ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input name="sale_price" type="text" id="sale_price" size="15">
<input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
<input name="button" type="submit" class="bordered" id="button" value="Calculate" />
</form>
and also refer this FIDDLE it will more helpful to you..

How to call php variable input text from another Php page

My question is how do I call input value of user selected which has show by php code from another php.
Normal simple way we get the input this way
$calendar_id_val = $_GET['calendar_id'];
and now it is not working:
For example Show.php, I have one form which show the values from Database and show the result with php variable with While Loop.
<input name="calendar_id" value="<?php echo $calendar_id;?>">
and when user is submit that form I will carry these user selected value and perform insert.php
While you are doing echo in the input use echo $calendar_id_val
You should use form like,
<form action="insert.php" method="get">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>"/>
<input type="submit" name="submit_id" value="Insert"/>
</form>
Insert.php
echo $calendar_id_val = $_GET['calendar_id'];
Does this answer your question?
<form action="insert.php" method="GET">
<input name="calendar_id" value="<?php echo $calendar_id;?>">
</form>
If you want to redirect the user back to show.php, add this to the end of your insert.php script
header('Location: show.php');
exit();
I suggest $_POST var like this:
<form action="insert.php" method="POST">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>" />
<input type="submit" name="submit" value="Submit" />
</form>
insert.php file:
echo $calendar_id = $_POST['calendar_id'];

how to send value to the from action page from database

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.

Categories