By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,838 Members | 2,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,838 IT Pros & Developers. It's quick & easy.

Getting a 404 error when posting a form to the same page as itself.

P: 26
Hello!

I am currently working on a project. And have been assigned to get up to speed quickly on php. And even though I love the language, it's not easy to get up to speed in like 2 seconds :-)

The code I will list below is supposed to have four parts:
1. Not logged in. List up all text from the database.
2. Logged in. List up all text from the database. But now with an "edit" button, with the possibility to jump to number 3.
3. Logged in, and pressed the edit button.
A form should show here, and the possibility to insert text into a field, and press submit.
4. Like number two, but now with the added text below.

The code I have is like this:

Expand|Select|Wrap|Line Numbers
  1. <?php include ("c:/db.inc");
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', '0');
  4.  
  5. //global $id = intval( mysql_real_escape_string( $_GET['id'] ));
  6. //global $itemid = intval( mysql_real_escape_string( $_GET['Itemid'] ));
  7.  
  8. if ($my->id && $my->id > 0) { //if logged in
  9. //this code tells me that I am logged in.
  10.     //show edit so I can show form
  11.  
  12. $query = "SELECT * FROM jos_ibruk_issues WHERE jos_issues.contentid= '$id' ORDER BY text desc";
  13.      $result = mysql_query($query);
  14.  
  15.      while($row= mysql_fetch_array($result)) {
  16.           echo"$row[text]<BR>";
  17.     }
  18.  
  19. } else { //not logged in, list up text
  20.  
  21. $query = "SELECT * FROM jos_issues WHERE jos_issues.contentid= '$id' ORDER BY text desc";
  22.      $result = mysql_query($query);
  23.  
  24.      while($row= mysql_fetch_array($result)) {
  25.           echo"$row[text]<BR>";
  26.     }
  27.     //mysql_close();
  28. }
  29.  
  30.  
  31. if ($my->id && $my->id > 0) { //&& $_POST['Submit'] == 'sumbmit') { //IF logged in and have pressed the EDIT "link"
  32.  
  33.     if($_POST['submit'] == 'Submit') {//if submit when have pressed EDIT link in number 2.
  34.         //echo "Submit has been pressed <BR>";
  35.         if(!$_POST['text'] || $_POST['text'] == "") {
  36.              echo "there is a problem. did you fill out anything in the form?<BR>";
  37.         }
  38.         else {
  39.              //all ok, insert into db.
  40.              $id = intval( mysql_real_escape_string($_GET['id']));
  41.              $as_text = addslashes($_POST['text']);
  42.              $tr_text = trim($as_text);
  43.              $query = "INSERT INTO jos_issues(ID,text,contentid)
  44.              VALUES(NULL,'$tr_text','$id')";
  45.              $result = mysql_query($query);
  46.              echo "thanks for posting these issues<BR>";
  47.               $id = intval( mysql_real_escape_string( $_GET['id'] ));
  48.              //echo "id = $id";
  49.              $query = "SELECT * FROM jos_issues WHERE jos_issues.contentid= '$id' ORDER BY text desc";
  50.              $result = mysql_query($query);
  51.              while($row= mysql_fetch_array($result)) {
  52.                   echo"$row[text]<BR>";
  53.              }
  54.         }
  55.     }
  56. $adresse = "http://localhost/demotest/index.php?option=com_content&task=view&" . "id=" . "$id" . "&Itemid=" . "$itemid";  
  57.  
  58. print <<<END
  59. <html>
  60. <form id="form1" name="frmname" method="post" action="<? echo $_SERVER['$adresse'];?">
  61. <label>Text
  62. <input type="text" name="text" />
  63. </label>
  64. <label>
  65. <input type="submit" name="submit" value="Submit" />
  66. </label>
  67. </form>
  68. </html>
  69. END;
  70. }
  71.  
  72. ?>
the forms POST ACTION Doesnt work. give me an error message. Website doesn't exists something.

Can you please help me with this? I have been sitting with this for several days now. I should have been finished with this already, and my boss is breathing down my neck. And he haven't brushed his teeth today :-)


Phropman
Aug 29 '07 #1
Share this Question
Share on Google+
9 Replies


pbmods
Expert 5K+
P: 5,821
Heya, "Phropman"

Changed thread title. Thanks for reading the Posting Guidelines!

Try this instead:
Expand|Select|Wrap|Line Numbers
  1. <form id="form1" name="frmname" method="post" action="<? echo basename(__FILE__);?">
  2.  
This will more reliably set the form action to the current page.
Aug 29 '07 #2

P: 26
Try this instead:
Expand|Select|Wrap|Line Numbers
  1. <form id="form1" name="frmname" method="post" action="<? echo basename(__FILE__);?">
  2.  
This will more reliably set the form action to the current page.
I tried setting [PHP]action="<? echo basename(__FILE__);?>">[/PHP] but all I get is: "The requested URL was not found on this server. The link on the ">referring page seems to be wrong or outdated. Please inform the author of ">that page about the error. If you entered the URL manually please check your spelling and try again."

I have to disagree with the title though: The user authentication is in place, but I need to sort out where the users should go after what they press. Wait, maybe that is user authentication after all. well...
Aug 29 '07 #3

pbmods
Expert 5K+
P: 5,821
Heya, Phopman

I have to disagree with the title though: The user authentication is in place, but I need to sort out where the users should go after what they press. Wait, maybe that is user authentication after all. well...
How's this?
Aug 29 '07 #4

P: 26
Heya, Phopman



How's this?
I don't get a 404 error when users logs in. I just need help to help the users see different things whether they are:

Not logged in (guest)
Logged in
Logged in and have pressed edit button

That's why I disagree
Aug 30 '07 #5

pbmods
Expert 5K+
P: 5,821
Heya, Phopman.

So when the User submits the form, it is correctly posting to a valid page... but when you try to redirect the User based on whether he is logged in, that's when you get the 'doesn't exist' error?
Aug 30 '07 #6

P: 26
It should post to itself. Which it doesn't do.

It gives me a blank page, with nothing on.
Aug 30 '07 #7

pbmods
Expert 5K+
P: 5,821
Heya, Phopman.

Okie. All set.

What does the form HTML look like when you view it in your browser and select 'View Source' from the menu?
Aug 30 '07 #8

P: 26
Since the boss threatened to kick my ass yesterday, I just did it an very non-elegant way. Reckons he will yell at me today but :-)

four pages
1. mainpage . list up from database. print out data with links to delete, insert and update pages.
2. delete. deletes from db where contentid = id. No questions asked, it just does :-)
3. Insert. Where you can insert new text in the textarea
4. update. Where you can update.

I think I will have to see today whether I can put everything into one page. It's actually a module in joomla I am working on, so I can only have php in that module, and that is only one php file.
Aug 31 '07 #9

HaLo2FrEeEk
100+
P: 404
I'd recommend a switch statement, and (if you're not already) using sessions to store the logged on status. You could also use AJAX to submit the form and handle the response.
Jan 4 '11 #10

Post your reply

Sign in to post your reply or Sign up for a free account.