if(isset($_POST['submit'])) not working - php

I know there are cases on this, however I tried their method, but it didn't work for me.
Here is the code for isset: I tried placing this statement before <head> tag and within <body> tag but don't think it made a difference. This code resides in the same php file as the the submit button.
<?php if(isset($_POST['action'])) {
echo "testing";
exit();
}?>
Here is the form & submit button within form
<form name="createCaseForm" method = "post" action = "<?php echo url_mgt::getActionURL(); ?>" id="case-form" novalidate="novalidate">
...
<td colspan="1"><button style="float:right;" name="action" type="submit" value="create_case_submit">Submit</button></td>
...
</form>
I'm not sure if using <button> instead <input> of would make a difference. I also did if(count($_POST) > 0) and it didn't work. I'm not sure what I'm doing wrong. When I run the file, it doesn't echo "testing", but instead went straight to the next page. My apologies if it's just a minor matter, but I tried methods posted in forums, but none seems to work.
I've tried to do this:
if(isset($_POST['action']) == 0) {
echo "Testing";
} else {
echo "Testing2";
}
It displays testing when the page is being loaded, however when i click on the submit button, I was hoping to see testing2 being printed. Am i right to say that after clicking the submit button, the page would not reload twice? So there is no way to check if the submit button is being posted?

I was having the same problem as above, if(isset($_POST['submit'])) not working when submitting a form. Setting the name to action didn't work for me. The solution was to assigned the submit to the name field of my form's input element. Here I present a MCVE tested in Firefox.
Kf
index.html
<html>
<body>
<form action="learnphp.php" method="post">
<table border="0">
<tr>
<td>Name</td>
<td align="center"><input type="text" name="username" size="30"/></td>
</tr>
<tr>
<td>City</td>
<td align="center"><input type="text" name="cityaddress" size="30"/></td>
</tr>
<tr>
<td><input id="radio1" type="radio" name="selector"
value="op1" /> </td>
<td><input id="radio2" type="radio" name="selector"
value="op2" /> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit"
name="submit" value="SubmiT"/></td>
</tr>
</table>
</form>
</body>
</html>
learnphp.php
<html>
<head>
<title>Information Gathered</title>
</head>
<body>
<?php
echo "<p>Data processed \"NOW\"</p>";
echo "<p>Let's observe: </p>";
if(isset($_POST['submit'])){
echo "submit set<br>";
echo "Submit value=" . $_POST['submit'] . "<br>";
}
else echo "ERROR submit not set<br>";
if(isset($_POST['selector'])){
echo "Selecter set<br>";
echo "Radial selection value=" . $_POST['selector'] . "<br>";
}
else echo "ERROR Selector not set<br>";
echo "</br>";
?>
</body>
</html>
Output:
Data processed "NOW"
Let's observe:
submit set
Submit value=SubmiT
Selecter set
Radial selection value=op2

You need a form field named action
<input type="text" name="action"/>
Also, make sure that url_mgt::getActionURL() is outputting the correct URL to submit to

Related

After form submission want to show success message in the form page

Admin side I submit form to admin-post.php. I want to print success message in the bottom of the form. Iam new in wordpress what i did shown below.
admin.php?page=add-products form code
<form action="<?php echo admin_url('admin-post.php') ?>" method="post">
<table>
<input type="hidden" name="action" value="add_product_from_admin">
<tr><td>Name</td><td><input type="text" name="pr_name" id="pr_name"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="pr_submit" id="pr_submit" value="Save Products"></td></tr>
</table>
</form>
add_action( 'admin_post_add_product_from_admin', 'add_product_into_data_base' );
function add_product_into_data_base() {
//some database operations
wp_redirect(admin_url('admin.php?page=add-products&message=success'));
}
This isn't so much a WordPress thing as a PHP thing. If you don't want to hide anything in the form, or submit/process the form with Ajax, you can just check to see if the &message parameter exists and echo it.
Also as an aside, you should be careful with your indentations - gotta clean that stuff up. It will make your life easier down the road. Here's a super basic example that will leave the form alone, unless &message is set as a query string parameter:
<form action="<?php echo admin_url('admin-post.php') ?>" method="post">
<table>
<input type="hidden" name="action" value="add_product_from_admin">
<input type="hidden" name="message" value="success">
<tr>
<td>Name</td>
<td><input type="text" name="pr_name" id="pr_name"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="pr_submit" id="pr_submit" value="Save Products"></td>
</tr>
</table>
<?= isset( $_POST['message'] ) ? $_POST['message'] : ''; ?>
</form>
This code will check if your hidden success action is present in data passed during page load (any time the form is submitted), and echo "Your Message".
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'success' ) {
echo 'Your Message';
}

php form image submit button - unable to submit

I am new to php. Though my doubt is very basic, but not understanding why my below form not able to submit? I am using actually image submit button.
Please tell me what wrong in below code:
<?php
function test(){
echo 'test';
}
var_dump($_POST['submit']); // here getting NULL, why?
if(isset($_POST['submit']))
{
test();
}
?>
<form action="." method=post name="loginform">
<table>
<tr>
<input type="password" style="display:none" />
<td width="130"><input type="password" name="password" size="15" maxlength="255" ></td>
<td width="136"><input type="image" src="button-log-in.gif" name="log in" alt=" log in" width="51" height="20" border="0"></td>
</tr>
</table>
</form>
There's no element which has 'submit' name. You're submitting two things, indicated by the name attributes: "password" and "log in". $_POST['...'] contains both of them.
If you'd like the function test() to be called upon submit, regardless of what is entered, you'd better add a hidden input field and check for its existence in PHP.
HTML:
...
<input type="hidden" name="some_name">
...
PHP:
if(isset($_POST['some_name']))
{
test();
}

How to echo a value from a form in HTML to a table on another page (using PHP)

I'm trying to echo a value from a HTML form to another page where I have a table, using PHP. Firstly, would I need any code before the table, e.g.
<?php
$_GET[hello.htm]
?>
<table>
etc...
Secondly, where would I put the PHP code? Inside each
<td></td> ?
Thirdly, how would the PHP be in order to retrieve the value from say the third box (you can type in them) in the form? Would it be:
$_GET[hello.htm,2]?
You need an action that get values as POST type and of course a SUBMIT trigger.
PHP codes can be put anywhere.
You can get each value from inputs by inputs' IDs or NAMEs.
you could show all sended values like so:
<?php
var_dump($_GET);
var_dump($_POST);
?>
Then when you know what exactpy you wana show (for example if this is in $_GET superglobal:
<?= $_GET['key_name']; ?>
or
<?php echo $_GET['key_name']; ?>
For example
<form action="other_file.php" method="POST">
<input type="text" name="my_input_name" />
<input type="submit" value="GO!" />
</form>
Will generate
$_POST['my_input_name']
So you could use it as:
<?php echo $_POST['my_input_name']; ?>
Maybe we need 2 PHP files
index.php
result.php
index.php
<html>
<body>
<form action="result.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
result.php
<html>
<body>
<h3> Result: </h3>
<table border="1">
<tr>
<td> Name </td>
<td> Email </td>
</tr>
<tr>
<td> <?php echo $_POST["name"]; ?> </td>
<td> <?php echo $_POST["email"] ?> </td>
</tr>
</table>
</body>
</html>
in index.php we use form action to send data to result.php
in result.php we use $_POST['form_input_name'] to get data from form input

How to send value of radio button to other page

I have a table with radio buttons to get the row values and 2 buttons
1 button.)For printing data , which moves to "notice.php"
2 button.)For row details,which stays on the same page.
<form action="" method="POST">
<table border="1" >
<tr>
<th>sourceID</th>
....
<th>Status</th>
</tr>
<tr>
<td><input type="radio" name="ID[]" value="<?php echo $tot; ?>" /></td>
<td>1</td>
....
<td>open</td>
</tr>
<input type="button" name="issue" value="Issue Notice" onClick="location.href='notice.php'" />
<input type="submit" name="details" value="details" />
<?php
if(isset($_POST['details']))
{
$n=$_POST['ID'];
$a=implode("</br>",$n);
echo$a;
}
Notice.php:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n=$_POST['ID'];
}?>
The problem here is: My code is working perfectly fine with the button details.
But it doesnt work with issue, i.e after selecting radio button and clicking on the issue notice button :it gives Undefined index: ID in D:\XAMPP\notice.php.
kindly help
Your details button is a submit button, so it submits the form. However your other button is just a regular button and you use javascript to send the browser to notice.php. As such, it does not post any data to notice.php.
You could include the data on the query string and send it that way, e.g.:
location.href="notice.php?id=<?=$tot?>"
Or you could also have the issue button post the page, and then have your receiving page check which submit button was used. If the issue button was used you could then have the php code post to notice.php.
Using the following code is the exact same as having a link:
<input type="button" name="issue" value="Issue Notice" onClick="location.href='notice.php'" />
As in, this will not change the form action and submit the POST data to your new page.
You would need something like:
<form method="post" action="" name="unique-form-name">
<input type="radio" name="ID[]" value="<?php echo $tot; ?>">
<input type="button" id="unique-btn-name" value="Issue Notice">
</form>
<script type="text/javascript">
document.getElementById('unique-btn-name').onclick = function(){
document['unique-form-name'].action='notice.php';
document['unique-form-name'].submit();
}
</script>
Then, once you get the data to notice.php, you'll have to use the data as an array (you won't be able to echo the data):
$IDs = $_POST['ID'];
echo '<pre>',print_r($IDs),'</pre>';
<input type="radio" name="ID" value="<?php echo $tot; ?>" />
Your error is the name attribute.
Also the other button is not related to the form at all. You may want to use ajax here.

Form in Form is not allowed, but what else

I fetch SQL data with a while loop and insert the data into a table. With a form and a submit button i can save modified values.
My simplified code:
<table>
<tr>
<?php
//SQL QUERY
//...
while ($row = mysql_fetch_array($sql_header)) {
$var_ab = $row['ab'];
$var_id = $row['id'];
?>
<form id="form1" action="" method="POST">
<td>
<input type="text" value="<?php echo $var_ab; ?>"
</td>
<td>
<input type="text" value="<?php echo $var_id; ?>"
</td>
//PLACEHOLDER FOR SECOND FORM
<?php
}
?>
</tr>
<td>
<input type="submit" name="save" value="SAVE" class="classsubmit" />
</td>
</form>
</tr>
</table>
So far, so good. So, how can I insert a second form to delete an entry? I've tried to place this code (PLACEHOLDER FOR SECOND FORM - see above)
<td>
<form id="form2" action="" method="POST">
<input type="text" value="<?php echo $var_id;?>"
</form>
</td>
but it's not working and it's not allowed to nest forms.
Any suggestions?
If you only want to delete an entry on the page or in the database you could try a button or a span with an onclick function.
for example:
<span onclick="window.location='?delete=<?php echo $row[(unique-value-in-database-from-this-row)]; ?>'; return false">Delete entry</span>
Make sure you add return false or the first form will be submitted. If you use a button make sure it has type="button"
On this page could be a PHP code like this:
if(isset($_GET['delete']))
{
$item = $_GET['delete'];
//SQL connect
$result = mysql_query($conection ,"DELETE FROM table WHERE uniquevalue='$item'");
}
I hope gives an idea for a solution.

Categories