Jump to content

problem with submit form in php


dansk

Recommended Posts

hello,

i have this page that is created using a post method, and i can't get the first if statment to be executed "see the comments by the if statment"

[code]<?

$connect = mysql_connect("localhost", "user","pass") or die(mysql_error());
$result = mysql_query("SELECT * from Announ.login WHERE session_id = '" . $_COOKIE[id] . "'");
$row = mysql_fetch_array($result);

if ($row[session_id] == $_COOKIE[id] && $row[user_name] == "admin")
{

  if($_POST['ID']==submit) ********//here's the problem, if I use $_POST, it will always go here since it comes from another page that uses a form with method post, how do i make it only go here when the form is submitted

  {


echo "in there<br>";
      // Set global variables to easier names

    // and prevent sql injection and apostrophe to break the db.

      $post_title = mysql_real_escape_string($_POST['post_title']);

      $post = mysql_real_escape_string($_POST['post']);
     
      $post_status = mysql_real_escape_string($_POST['post_status']);
     
      $image = mysql_real_escape_string($_POST['image']);

      $image_link = mysql_real_escape_string($_POST['link']);

              //check if (title) field is empty then print error message.

              if(!$post_title){  //this means If the title is really empty.

                    echo "Error: News title is a required field. Please fill it.";

                    exit(); //exit the script and don't do anything else.

              }// end of if





        $result = mysql_query("UPDATE announ.articles SET Post_Title='$post_title', Post_Date='NOW()', Post='$post',Images='$image', Image_Link='$image_link' WHERE ID=' " . $_POST['ID'] . " ' ")or die(mysql_error());


 
          echo "<b>Thank you! News UPDATED Successfully!<br>You'll be redirected to Home Page after (2) Seconds";

          echo "<meta http-equiv=Refresh content=2;url=Posts.php>";

}

elseif($_POST['ID'])


{



        $result = mysql_query("SELECT * FROM announ.articles WHERE ID= ' " . $_POST['ID'] . " ' ");

        while($myrow = mysql_fetch_assoc($result))

            {

                $post_title = $myrow["Post_Title"];

                $post = $myrow["Post"];

                $post_status= $myrow["Post_Status"];

                $image = $myrow["Image"];

$image_link =$myrow["Image_Link"];
?>

<br>


<h3>::Edit News</h3>


<form method="POST" action="<?php echo $PHP_SELF ?>">

<input type="hidden" name="ID" value="<? echo $myrow['ID']?>">



Title: <input name="post_title" size="40" maxlength="255" value="<? echo $post_title; ?>">

<br>

News: <textarea name="post"  rows="7" cols="30"><? echo $post; ?></textarea>

<br>

Status: <input name="post_status" size="10" maxlength="255" value ="<? echo $post_status; ?>">

<br>

image_link: <input name="image_link" size="20" maxlength="255" value ="<? echo $image_link; ?>" >

<input type="submit" name="submit" value="Update News">

</form>

<?

              }//end of while loop



  }//end else
 
  }
else
{
header("Location: login.php");
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/32936-problem-with-submit-form-in-php/
Share on other sites

short answer. [code=php:0]if($_POST['ID']) //do this[/code] should work, or I've seen it written this way: [code=php:0]if(is_set($_POST['ID'])) //do this [/code].

longer answer. If you're visiting the page from a link, the [code=php:0]if($_POST)[/code] will return false. If you submit a form (with the method="post"), it will return true. If you're [code=php:0]include[/code]ing from a page that is beign posted to, [code=php:0]if($_POST)[/code] will still return true.

does that make sense?
michaellunsford, Thank you for the help

I understand you proint. But, I only would like it to return true when I submit the form on that page itself. Since I am visiting from a different page where I process a POST method. Your answer makes it true which directly goes in the first if statment - notice i have two if statements- one is in the middle which will display text fields and a POST form to be submitted, only then, i want the first If to be true.

Thank you very much, I really appericate your help

if your second form has a unique field name, just check for that. [code=php:0]if($_POST['different_field'][/code] That way, it will only be true if the second form is posted. Don't have a second field? Just put a hidden field inside that second post form.
[code=php:0]<input type="hidden" name="second_post" value="whatever">[/code] When you check for that field, it will only be true if it's present (on the second form post).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.