|
-
Aug 6th, 2009, 03:23 AM
#1
Thread Starter
Addicted Member
[RESOLVED] $_POST[''] or not to $_POST['']
HTML Code:
<form method="post" name="postme">
<table width="100" >
<tr>
<td width="50"><input type="text" name="Textbox 1" /></td>
<td width="50"><input type="text" name="Textbox 2" /></td>
</tr>
<tr>
<td width="50"><input type="submit" name="B1" value="Button 1"/></td>
<td width="50"><input type="submit" name="B2" value="Button 2" /></td>
</tr>
</table>
</form>
PHP Code:
if($_POST['B1']){ echo "button 1 Pressed"; } if($_POST['B2']){ echo "button 2 pressed"; }
There are two problems I am having with '$_POST['']' and I don't know if there is a way to solve them?
1st - if a button is submitted then the page is refreshed (possibly by a button on a different form?) it gets re-submitted
2nd - if a textbox is submited (ie with the enter key) the last button submitted is re-submitted
I probably just being pickie but If anyone knows away around this, or is it me just getting these???
Thanks
-
Aug 6th, 2009, 04:55 AM
#2
Re: $_POST[''] or not to $_POST['']
Try this:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
if ($_POST["B1"]){
echo "button 1 Pressed";
}
if ($_POST["B2"]){
echo "button 2 Pressed";
}
$conn = @mysql_connect("localhost", "root", "");
if (!$conn) {
die("Connection failed: " .mysql_error());
}
mysql_select_db("buttons", $conn);
$query = "INSERT INTO pressed (B1, B2)
VALUES ('$_POST[B1]', '$_POST[B2]')";
if (mysql_query($query, $conn)){
}else {
die(mysql_error());
}
?>
<table width="100" >
<tr>
<form method="post" name="postme">
<td width="50"><input type="text" name="B1" /></td>
<td width="50"><input type="text" name="B2" /></td>
<td width="50"><input type="submit" name="B1" value="Button 1"/></td>
<td width="50"><input type="submit" name="B2" value="Button 2"/></td>
</form>
</tr>
</table>
</body>
</html>
When you press "Button 1" the Button 1 echo is called and with Button 2 the Button 2 echo is called. This method uses a database to control the action because "post" is a command used to send data usually to a database.
Edit:
This is the code for the database:
-- phpMyAdmin SQL Dump
-- version 3.2.0.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Aug 06, 2009 at 10:01 AM
-- Server version: 5.1.36
-- PHP Version: 5.3.0
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `buttons`
--
-- --------------------------------------------------------
--
-- Table structure for table `pressed`
--
CREATE TABLE IF NOT EXISTS `pressed` (
`B1` int(1) NOT NULL,
`B2` int(1) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
Last edited by Nightwalker83; Aug 6th, 2009 at 05:04 AM.
Reason: Adding more
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Aug 6th, 2009, 02:36 PM
#3
Re: $_POST[''] or not to $_POST['']
I'm not sure how your solution relates to his problem at all.
to solve the problem of post data being resubmitted, do a redirect. I usually post my form to a separate script that handles -only- data verification and then redirect back if everything is okay -- otherwise, I just include the other script and define some kind of error variable to let the user know something was wrong. for example, this is the processing script:
process.php
PHP Code:
<?php if($_SERVER['REQUEST_METHOD'] != "POST"){ header("Location: form.php"); }
$errors = array();
//validation, make sure nothing was empty/do other stuff foreach($_POST as $key => $value){ if(empty($value)){ $errors[] = "{$key} was empty"; } }
if(count($errors) == 0){ //this is where you'd update your database or do whatever you needed to do // .....
//now redirect header("Location: form.php"); }
//if we get to this point, there must have been some errors. //include the form include("form.php"); ?>
and then form.php:
PHP Code:
<?php //were there any errors? if(isset($errors)){ ?> There were a few errors: <ul> <?php foreach($errors as $error): ?> <li><?php echo $error; ?></li> <?php endforeach; ?> </ul> <?php } ?>
<form action="process.php" method="post">
<input type="text" name="whatever" value="<?php echo htmlentities($_POST['whatever']); ?>" />
<input type="submit" name="b1" value="Button 1" /> <input type="submit" name="b1" value="Button 2" />
</form>
obviously, the processing script would need to be changed; I'd recommend using a switch(), for example. you should get the idea though!
as far as your second problem goes, I don't really have a solution. I never submit forms by hitting enter anymore unless they're "single-button" forms. using javascript, you might be able to control what happens when you hit enter, though.
-
Aug 6th, 2009, 09:12 PM
#4
Re: $_POST[''] or not to $_POST['']
 Originally Posted by kows
I'm not sure how your solution relates to his problem at all.
I found it difficult to tell what Lingo Outsider actually wanted! It seemed to me that the problem he was having with the page refresh was because he had two submit buttons and they were in conflict.
Edit:
Your code does not work all it does is output the error to the text box! However, considering how vague the original question is that is understandable. It seems we both have two completely different answers to a question that is not very clear.
Last edited by Nightwalker83; Aug 6th, 2009 at 09:25 PM.
Reason: Adding more
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Aug 7th, 2009, 02:59 AM
#5
Thread Starter
Addicted Member
Re: $_POST[''] or not to $_POST['']
Thanks for your help and sorry for the vague question.
your corret in that my buttons are in conflict. and my samples form should have been set to get although it could work with post.
1st... A button appears in the URL as being submited when pressed.
Then... The text from the textarea appears in the URL as well as the previously pressed button when the enter button is pressed inside the textarea.
so if I use $_GET and I think $_POST it shows as submited even though It hasn't.
-
Aug 7th, 2009, 03:27 AM
#6
Re: $_POST[''] or not to $_POST['']
 Originally Posted by LingoOutsider
Thanks for your help and sorry for the vague question.
your corret in that my buttons are in conflict. and my samples form should have been set to get although it could work with post.
Have you tested the code I posted above?
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Aug 7th, 2009, 05:07 AM
#7
Re: $_POST[''] or not to $_POST['']
Your code does not work all it does is output the error to the text box! However, considering how vague the original question is that is understandable. It seems we both have two completely different answers to a question that is not very clear.
the code I posted addresses his first problem; it works fine. I'm not sure what you expected it to do, but all it does is stop a user from re-submitting form data after a successful submission (by forcing a redirect after a successful submission). this could somewhat be solved by submitting via GET instead, though.
I'm still not sure how the code you posted solves anything though. it's just a replica of his own code, but you insert the data into a database for some reason (but never do anything with it?). maybe I'm just missing something :X
-
Aug 7th, 2009, 05:43 AM
#8
Re: $_POST[''] or not to $_POST['']
 Originally Posted by kows
I'm still not sure how the code you posted solves anything though. it's just a replica of his own code, but you insert the data into a database for some reason (but never do anything with it?). maybe I'm just missing something :X
The code I posted displays a message saying which button was clicked! Which by looking at his code LingoOutsider was trying to do? I removed the second "Submit" and replaced it with test to see which value "B1" or "B2" was sent then display the appropriate message.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Aug 8th, 2009, 04:52 PM
#9
Re: $_POST[''] or not to $_POST['']
..?
the code HE posted displays a message saying which button was clicked, and the code you posted still has two submit buttons, and just has the same block of code to display a message. you're also naming the text boxes and submit buttons the same thing. you should go back and take a look at what you posted :/ because you seem to be thinking you posted something much different..
-
Aug 8th, 2009, 07:30 PM
#10
Re: $_POST[''] or not to $_POST['']
 Originally Posted by kows
..?
the code HE posted displays a message saying which button was clicked, and the code you posted still has two submit buttons, and just has the same block of code to display a message. you're also naming the text boxes and submit buttons the same thing. you should go back and take a look at what you posted :/ because you seem to be thinking you posted something much different..
Oops, you are correct! I was thinking of the code I tested that didn't work out the way I was hoping.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Aug 9th, 2009, 11:46 AM
#11
Re: $_POST[''] or not to $_POST['']
 Originally Posted by Nightwalker83
I found it difficult to tell what Lingo Outsider actually wanted! It seemed to me that the problem he was having with the page refresh was because he had two submit buttons and they were in conflict.
Edit:
Your code does not work all it does is output the error to the text box! However, considering how vague the original question is that is understandable. It seems we both have two completely different answers to a question that is not very clear.
If you are not sure what the original poster is asking, either leave to to someone else to answer or ask the OP to clarify. Your post just confused the situation
-
Aug 9th, 2009, 11:56 AM
#12
Re: $_POST[''] or not to $_POST['']
 Originally Posted by LingoOutsider
Thanks for your help and sorry for the vague question.
your corret in that my buttons are in conflict. and my samples form should have been set to get although it could work with post.
1st... A button appears in the URL as being submited when pressed.
Then... The text from the textarea appears in the URL as well as the previously pressed button when the enter button is pressed inside the textarea.
so if I use $_GET and I think $_POST it shows as submited even though It hasn't.
Having two submit buttons does not mean that they are in conflict; as long as you realise that only one button can be used to submit the form in any one request and you require your application to behave differently depending on which button is pressed, it is fine. The Google search enginge has two submit buttons "Search" and "I'm Feeling Lucky".
When testing, it is best to use the Isset() construct rather than simply testing weather or not it is "true". The fact you are doing that also indicates that you have error_reporting set to something other than "E_ALL". During the development of your script or application, it is essential that you set error reporting to its maximum level. You can do this by changing php.ini:
Code:
error_reporting = E_ALL
Or set it at the top of the script:
PHP Code:
error_reporting(E_ALL);
Doing this will help with the debugging process because it will spit out a notice every time you try and read a variable that was never set. A great way of filtering out typos and encouraging secure coding by ensuring that any variable you intend to use is explicitly set first in the code:
PHP Code:
$myvar = "";
$mayvar += "Hello"
-
Aug 9th, 2009, 08:10 PM
#13
Re: $_POST[''] or not to $_POST['']
 Originally Posted by visualAd
If you are not sure what the original poster is asking, either leave to to someone else to answer or ask the OP to clarify. Your post just confused the situation 
That's what I did but after I had already posted the code example! By the time I thought about the question someone had already replied to the thread.
 Originally Posted by visualAd
Having two submit buttons does not mean that they are in conflict;
Ah ok, I was thinking that just because it was a submit button that it was type controlling it.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|