Displaying HTML form values on PHP - php

This is test.html
<!DOCTYPE html>
<html>
<body>
<form method="POST" action="test.php">
<table>
<tr>
<th>content_id</th>
<th>title</th>
<th>image</th>
</tr>
<tr>
<td><input type="text" name="content_id" size="26"></td>
<td><input type="text" name="title" size="26"></td>
<td><input type="text" name="image" size="26"></td>
</tr>
</table>
<input type="button" id="cancel" name="cancel" value="Cancel"> <a href="index.php"/></a>
<button type="submit" id="submit" value="Submit">Submit</button>
</form>
</body>
</html>
This is test.php
<?php
error_reporting(E_ALL);ini_set('display_errors','1');
echo $_POST["content_id"];
echo $_POST["title"];
echo $_POST["image"];
?>
I want to pass the form's value to the PHP and display it but it shows this instead.
EDIT: I apologize for the vague and confusing question, thanks for the assistance everyone!

An error (500) occurs because your syntax is invalid. You can't mix text and commands.
There are two methods of sending data to PHP; GET and POST. GET sends information in URL parameters, and POST sends data in headers. You must use the appropriate PHP method to get data in each format. Given you set the method in the HTML form as POST, you should use _POST in PHP:
<?php
echo "Welcome ".$_POST["content_id"];
echo "Welcome ".$_POST["title"];
echo "Welcome ".$_POST["image"];
?>
In the future, you should look at the error_log, or enable error reporting so that you can see the actual results from the PHP runtime. Details on this can be found here.

The 500 error is because Welcome is not executable PHP code. Put it outside the <?php ?> tags

Your form method is using GET, your test.php should be:
<?php
echo $_POST["content_id"];
echo $_POST["title"];
echo $_POST["image"];
?>
And your form misses its end tag and a submit button:
<form method="POST" action="test.php">
<table>
<tr>
<th>content_id</th>
<th>title</th>
<th>image</th>
</tr>
<tr>
<td><input type="text" name="content_id" size="26"></td>
<td><input type="text" name="title" size="26"></td>
<td><input type="text" name="image" size="26"></td>
</tr>
</table>
<button type="submit">Submit</button>
</form>
Here's the form screenshot: https://imgur.com/a/3ST9k
And here's the php script result: https://imgur.com/a/YUUl4

Related

How to update the data in particular table row in HTML?

Since the <form> can not inside <tr>, I can not assign the form for each row. And at the end I have create a from like this
<form>
<table>
<tr><td><input name="my_product_name"></td><td><button type="submit"></td></tr>
<tr><td><input name="my_product_name"></td><td><button type="submit"></td></tr>
</table>
</form>
So, it seems when I submit data it submit every input, how can I send only a specific row? Thanks
Since forms send all the values, (since your using <button>), you could assign each row a key that corresponds to that row. Then use that in the textbox. Note: Textbox values must be an array structure, so that particular key from the submit update can be used (kinda like filtering). Consider this example:
<?php
if(isset($_POST['update'])) {
$product_key = $_POST['update'];
$product_name = $_POST['my_product_name'][$product_key];
echo $product_name;
// this should correspond to that same textbox row that you selected
}
?>
<form method="POST" action="">
<table>
<tr>
<td><input type="text" name="my_product_name[1]"></td>
<td><button name="update" type="submit" value="1">Update</button></td>
</tr>
<tr>
<td><input type="text" name="my_product_name[2]"></td>
<td><button name="update" type="submit" value="2">Update</button></td>
</tr>
</table>
</form>
This is not possible. A form sends everything in it. You have to use multiple forms in order to do that. Can you explain why you want to send each row separately. Let us know the complete problem so we can provide you much better solution :)
You must use separate forms. To avoid this issue, you could try using div's and style them with css.
<div class="big-wrapper">
<div class="form-wrapper">
<form id="first-input">
<input name="my_product_name">
<button type="submit">Click Here To Submit Form 1</button>
</form>
</div>
<div class="form-wrapper">
<form id="second-input">
<input name="my_product_name">
<button type="submit">Click Here To Submit Form 2</button>
</form>
</div>
Example of above code (with no additional css): http://postimg.org/image/p0olzl933/
One method would be to have form tags that wrap tables and each table effectively becomes a row.
Honestly, there are a lot of ways to do this... you could handle it using Javascript with something like I have below or generating a special table with a server-side script and writing that to the page. You could use ajax calls to send the values that change to a web service, etc.
Hope that thelps.
<form>
<table>
<tr><td><input name="my_product_name"></td><td><button type="submit"></td></tr>
</table>
</form>
<form>
<table>
<tr><td><input name="my_product_name"></td><td><button type="submit"></td></tr>
</table>
</form>
...
Collect the value then push it into the form on submit.
<script>
function dosubmit(e){
var value = $(this).parents('tr').find('input.some').val();
$(this).parents('tr').find('input.my_product_name').val(value);
}
</script>
<table>
<tr>
<td><input name="some"/></td>
<td>
<form onsubmit="dosubmit(event)">
<input type="hidden" name="my_product_name"/>
<input type="submit" value="Submit">
</form>
</td>
</tr>
<tr>
<td><input name="some"/></td>
<td>
<form onsubmit="dosubmit(event)">
<input type="hidden" name="my_product_name"/>
<input type="submit" value="Submit">
</form>
</td>
</tr>
<tr>
<td><input name="some"/></td>
<td>
<form onsubmit="dosubmit(event)">
<input type="hidden" name="my_product_name"/>
<input type="submit" value="Submit">
</form>
</td>
</tr>
</table>
I don't know how much good a jsbin will be, but here it is
You can submit the value in form by setting in hidden parameter as follows:
HTML:
<form action="" id="formId">
<input type="hidden" name="product_name" id="hiddenValue" />
</form>
<table>
<tr>
<td><input name="my_product_name"></td>
<td><button type="submit" onclick="submitForm(this)"></td>
</tr>
<tr>
<td><input name="my_product_name"></td>
<td><button type="submit" onclick="submitForm(this)"></td>
</tr>
</table>
Script:
<script>
function submitForm(id){
var input = id.parentElement.parentElement.getElementsByTagName("input")[0].value;
//Retrieving value of specific rows
document.getElementById("hiddenValue").value = input;
//Submits the form with given id.
document.getElementById("formId").submit();
}
</script>

how to send data of all the forms in a page using one submit button?

Here is my index.php
<!doctype html>
<html>
<head>
<title>welcome to vikash general shop</title>
<link rel="stylesheet" type="text/css" href="css/table_styling.css">
</head>
<body>
<table>
<tr>
<td>Item</td>
<td>Amount(kg)</td>
<td>Amount(gm)</td>
</tr>
<tr>
<td>Sugar</td>
<td><form method="POST" action="process.php"><input type="text" name="sugar_amount_kg"/></form></td>
<td><form method="POST" action="process.php"><input type="text" name="sugar_amount_gm"/></form></td>
</tr>
<tr>
<td>Rice</td>
<td><form method="POST" action="process.php"><input type="text" name="rice_amount_kg"/></form></td>
<td><form method="POST" action="process.php"><input type="text" name="rice_amount_gm"/></form></td>
</tr>
</table>
<form method="POST" action="process.php">
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
actually i want to send data of all the forms in page using one submit button it is not working. It's obvious because submit button is only sending it's own form tag(quite selfish :P). So i want to know how to send data of all the forms using one submit button...
or if you have any other solution for my code then please tell me...
Just make one form. Wrap your table with form tags, because it's all being processed by process.php anyway.
<form method="POST" action="process.php">
<table>
<tr>
<td>Item</td>
<td>Amount(kg)</td>
<td>Amount(gm)</td>
</tr>
<tr>
<td>Sugar</td>
<td><input type="text" name="sugar_amount_kg"/></td>
<td><input type="text" name="sugar_amount_gm"/></td>
</tr>
<tr>
<td>Rice</td>
<td><input type="text" name="rice_amount_kg"/></td>
<td><input type="text" name="rice_amount_gm"/></td>
</tr>
</table>
<input type="submit" name="submit" value="submit" />
</form>
You do not need to add the form tags multiple times. Just wrap it around the input fields and add the action attribute in it like so:
<table>
<form action="process.php" method="post">
<tr>
<td>Item</td>
<td>Amount(kg)</td>
<td>Amount(gm)</td>
</tr>
<tr>
<td>Sugar</td>
<td><input type="text" name="sugar_amount_kg"/></td>
<td><input type="text" name="sugar_amount_gm"/></td>
</tr>
<tr>
<td>Rice</td>
<td><input type="text" name="rice_amount_kg"/></td>
<td><input type="text" name="rice_amount_gm"/></td>
</tr>
<input type="submit" name="submit" value="submit" />
</form>
</table>
And then you can get the inputs in process.php as follows:
if(isset($_POST['submit'])){ //checking if form was submitted
$sugar_amount_kg = $_POST['sugar_amount_kg'];
...
}
Hope this helps!
This question has been asked a few times. Do a quick search and you will see ways of doing this with JS/jQuery. Examples here and here.
Doing it in one form really might make more sense though for your specific use.

Submitting form from different <div> HTML

i have problem with submitting a form using 2 defferent div(tabs). I want to submit all the value from this form.. but when i submit, the value that i receive only from (div class="tab1") not from div class="tab2". how can i submit all the value even using different div
page.php
<div class="tab1">
<h2>Basic Information</h2>
<form name="pages_details" method="post" action="pages/save_pages.php">
<table>
<tr>
<td>Name:</td>
<td><input name="name" type="text" ></input></td>
</tr>
<tr>
<td>Order:</td>
<td><input name="ord" type="text"></input></td>
</tr>
</table>
</div>
<div class="tab2">
<h2>Additional Information</h2>
<table>
<tr>
<td>Special:</td>
<td><input name="special" type="text" ></input></td>
</tr>
<tr>
<td>Title:</td>
<td><input name="title" type="text"></input></td>
</tr>
</table>
</div>
<input type="submit" name="save" value="save">
</form>
save_pages.php
<?php
$name = $_REQUEST['name'];
$ord = $_REQUEST['ord'];
$special = $_REQUEST['special'];
$title = $_REQUEST['title'];
echo $name;
echo $ord;
echo $special;
echo $title;
?>
Try to make sure you close tags in the right order.
Structuring code like this can cause some weird bugs.
Not right:
<div>
<form>
</div>
</form>
So you might want to do it like this.
<form>
<div>
</div>
</form>
I would suggest to use jquery ajax to submit the form based on your case.
For example:
var datas = $(form).serialize();
$.post("pages/save_pages.php", datas, function(){
});
Referring:
post - http://api.jquery.com/jQuery.post/
serialize - http://api.jquery.com/serialize/

IE8 has no $_POST data

I am currently working on a login page, using Dreamveaver CS4.
My form looks like this (I have kept the code complete):
<form id="login" name="login" method="POST" action="<?php echo $loginFormAction; ?>"
enctype="multipart/form-data">
<input type="hidden" name="loginnow" id="loginnow" value="go">
<table border="0" cellpadding="5" cellspacing="0">
<tr>
<td rowspan="4"><img src="images/icons/login_icon.jpg" width="240" height="218" alt="login-icon"></td>
<td>Benutzername</td>
<td><input name="usern" type="text" class="fieldSm" id="usern"></td>
</tr>
<tr>
<td>Passwort</td>
<td><input name="passwrd" type="password" class="fieldSm" id="passwrd"></td>
</tr>
<tr>
<td colspan="2" align="center"><input name="submit" type="submit" class="button" id="submit" value="Anmelden"></td>
</tr>
<tr>
<td colspan="2">Hier können Sie sich registrieren</td>
</tr>
</table>
</form>
Now the php script checks for the hidden field
<?php if (isset($_POST['loginnow'])) {
# do some DW magic ;)
}
?>
With Firefox it works nicely and I can login properly, however is fails with IE8.
So, I was checking with:
<pre>
<?php print_r($_POST); ?>
</pre>
This results in and empty array when using IE8. $_REQUEST only has only the PHPSESSID.
I have searched several sites, with no results or hints (mostly the issues are with graphical submit buttons).
If anyone could give a hint where my error is, I would be very grateful.
All of your pages are with the suffix php? are you using a mamp/wamp or real webserver with php?

Troubles with html-forms

index.php:
<form action="update_db.php" method="post">
<?php
require_once 'modules/' . $currentModule . '.php';
?>
</form>
modules/some_module.php
...
<input type="submit" />
...
update_db.php:
#extract( $_POST );
print_r( $_POST );
After loading index.php i see need form. But during submitting i'm coming to the same page (index.php). Why?
http:/****/admin/
Here is html-code generated: http://dpaste.com/93396/
It's so strange, but form generates 2 times... I removed all part of code and rewrited it. Now everything is fine. Thanks all.
I took a look at your site. Your form action is index.php and that is why you keep seeing the same page after you click submit. If your code above is correct, ensure that you do not have <form> tags in your module containing the submit button.
<form action="index.php" method="post">
<table align="center">
<tr>
<td>Логин: </td>
<td><input type="textfield" name="login" /></td>
</tr>
<tr>
<td>Пароль: </td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" name="submit" value="вход" /></td>
</tr>
<table>
</form>
you have this:
<form action="index.php" method="post">
not this:
<form action="update_db.php" method="post">
Change it and your form will post to update_db.php

Categories