简体   繁体   中英

PHP-page and HTML-form submits look so messy, how to avoid submits to SELF?

I have a classifieds webbsite, and users have the option to change / remove their classifieds.

I am working on a "edit.php" page where all this can be done.

Basically it is setup like this when you click on "edit/remove classified":

A new page appears, with a form, where user may chose from "REMOVE" and "CHANGE" through radios. The user must enter a password which is connected to the classified. Then they hit submit.

The Form is submitted to itself, and checks which radio is selected, and checks if password was correct.

If the password is wrong, then the same page appears again, but with a message saying, "wrong password".

If the password is right, then based on if the user wants to "REMOVE" or "CHANGE" the classified, I want a completely new page to open.

My question is, would it be possible ans secure to open a new page based on the radio selection, via php?

Otherwise I would have to put so many if/else statements into the HTML and showing different forms with different actions, that it would be so messy.

So in PHP I would like to do this:

<?php
  if($password==$row['pass']){
    if($action == "remove"){ Open new page and DO remove }
    else if ($action == "change"){ Open new page and change the classified }
  }
?>

I use session for storing variables.

Is it possible, and secure to open a new page and just remove the classified for example? Or would it be safer to have multiple forms and display them based on the php form selections and password errors?

Hard to explain, but I want simply to avoid alot of if/else in my HTML. Otherwise it would be something like:

 <body>
   <?php if($pass_wrong==1): ?>
     FORM COMES HERE, AND STATES PASSWORD WAS WRONG
   <?php endif; ?>

   <?php if($action=='remove' && $pass_wrong==0): ?>
     CLASSIFIED SUCCESSFULLY REMOVED
   <?php endif; ?>

   <?php if($action=='change' && $pass_wrong==0): ?>
     Show a huuuuuge form to change the classified
   <?php endif; ?>

   ETC ETC

听起来您只需要知道如何执行HTTP重定向即可: http : //php.net/manual/en/function.header.php

One way of managing where your code is, is for each of your forms to have a hidden field describing what the form does. Then you can check what form was submitted back and include another php file to deal with it. This kind of thing:

<?php

    switch ($_POST['action']) {

        case "remove":
                include ("remove.php");
        break;

        case "change":
                include ("change.php");
        break;

        case "edit":
                include ("update.php");
        break;

        default:
                include ("edit.php");
        break;
    }
?>

This way you ought to be able to separate your code from each other. These other php files you are including could contain just a form or just the code to process the form. Either way, it's a lot easier to maintain code this way. There are many patterns of organising code and this is just one of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM