Forum Moderators: coopster

Message Too Old, No Replies

problems with $ GET and $ POST to do stuff on page

         

surrealillusions

10:47 am on Aug 4, 2010 (gmt 0)

10+ Year Member



Hi all,

I have a form that displays stuff from a database, for a user to edit. (user with admin status).

That form gets the data from a $_GET variable in the url, such as page.php?item=20

Now, that works ok, the problem comes from when I need to update the database.

The form submits to page.php, so I put:

if (isset($_POST['formsubmitted'])) {
// update database here
}

if (isset($_GET['item'])) {
//do stuff here
}

else {
// nothing!
}

The checking of the $_POST variable produces a blank page when its put into the page.

Even if i switch the statements around and make a big if and elseif statement, still nothing. If i take out the if statement about the formsubmitted and update database, then page displays fine.

How do I get this working and is there a better way of going about this?

Thanks.

morehawes

11:44 am on Aug 4, 2010 (gmt 0)

10+ Year Member



The problem is the form you are submitting can only do POST or GET - not both. Decide in your HTML which you are going to use :

<form method="****">


Then change your PHP accordingly. I hope that helps.

surrealillusions

12:13 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



The form is setup as post, and submits to the same page.

The GET function is getting the url variable, which is a normal <a> link on another page.

morehawes

12:19 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



Ah OK, if you are just getting a blank page try putting this at the top of your script to see if you are getting any error messages :


error_reporting(E_ALL);
ini_set("display_errors", 1);


Also just to check that you do have a hidden form input field for "formsubmitted"?

Matthew1980

12:33 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

The single most easiest method for checking $_POST or $_GET on a receiver script (the file specified in the action attribute of the form tag) is using this:

<?php
echo "<pre>";
print_r($_POST);
print_r($_GET);
echo "</pre>";
exit;

then from that you can see what element names are being transmitted from the form or the url, then once you have that info you can do the handling:-

if(isset($_POST['your_form']) && !empty($_POST['your_form'])){
//form has been submitted handle the data and process
}
else{
//redirect back to form, or throw an error
}

Just alter that example to cater for $_GET array if set from the URL.

And as morehawes correctly points out, any development work needs error reporting ON, this will help you loads in the longrun

error_reporting(E_ALL|E_DEPRECATED);

if you want to you can check for warnings on functions that are about to be made depricated (eregi > php5.3 *I think*)

Hope that makes sense.

Cheers,
MRb

morehawes

12:59 pm on Aug 4, 2010 (gmt 0)

10+ Year Member




echo "<pre>";
print_r($_POST);
print_r($_GET);
echo "</pre>";


Nicely pointed out Matthew1980 - print_r is my best friend :)

surrealillusions

2:45 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



Thanks for the help everyone.

Problem is solved and now have a working script.

:)

rocknbil

5:57 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the form you are submitting can only do POST or GET - not both.


Actually, not *exactly* true - although I would never do this, I see it done this way a lot:

<form method="post" action="somescript.php?val=Oops+I+am+in+get">

It creates the very problems you mention, something in both post and get, but - at the same time. Yuk.

morehawes

6:15 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



Actually, not *exactly* true - although I would never do this, I see it done this way a lot:


Interesting rocknbil - I had never even considered that!

Matthew1980

6:44 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

<form method="post" action="somescript.php?val=Oops+I+am+in+get">

Yes, I can admit to that.. When I first started out I was doing exactly that, but I soon found that just referring to a file for processing only in the action attribute made life a lot easier.

Cheers,
MRb