Jump to content

_POST['submit'] is not work, thank you for help!!!!


kindtiger

Recommended Posts

All my friends,

 

the  if(isset($_POST['submit'])) is not work, no matter what i have tried.

it seems very simple problem, but i can't get a clue to solve it.

 

Please help me !!!!!!! thank you in advance!!!!!!!!!

 

 


<?php
require("config.php");

if(isset($_GET['id']) == TRUE) {
  if(is_numeric($_GET['id']) == FALSE) {
   $error = 1;
  }
  else
  {
	$error =0;  
   }
  if($error == 1 ) {
	 header("Location: " . $config_basedir);
  }
  else {
	$validentry = $_GET['id'];
 }
}
else {
  $validentry = 0;
}

if(isset($_POST['submit'])){

	  $db = mysql_connect($dbhost, $dbuser, $dbpassword);
	  mysql_select_db($dbdatabase, $db);
	  
	  $sql = "INSERT INTO comments(blog_id, dataposted, name, comment) VALUES(" . 
			$validentry . ", NOW(), '" . $_POST['name'] . "', '" . $_POST['comment'] . "');";
	mysql_query($sql);

	header("Location: http://" . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . 
	"?id=" . $validentry);
}

else{

	require("header.php");

	if($validentry == 0) {
		$sql = "SELECT entries.*, categories.cat FROM  entries, categories " . 
		"WHERE entries.cat_id = categories.id " . 
		"ORDER BY dateposted DESC " .
		" LIMIT 1;";
	}
	else {
		$sql = "SELECT entries.*, categories.cat FROM  entries, categories " . 
		"WHERE entries.cat_id = categories.id AND entries.id = " . $validentry . 
		" ORDER BY dateposted DESC LIMIT 1;";
	}
	$result = mysql_query($sql);
	$row = mysql_fetch_assoc($result);
	echo "<h2>" . $row['subject'] . "</h2><br />";

	echo "<i>In <a href='viewcat.php?id=" . $row['cat_id'] . "'>" . $row['cat'] . 
	"</a> -  Posted on " . date("D jS F Y g.iA", strtotime($row['dateposted'])) . "</i>";

	echo "<p>";
	echo nl2br($row['body']);
	echo "</p>";

	$commsql = "SELECT * FROM  comments WHERE blog_id = " . $validentry . 
		" ORDER BY dateposted DESC;";
	$commresult = mysql_query($commsql);
	$numrows_comm = mysql_num_rows($commresult);

	if($numrows_comm ==0) {
	   echo "<p> No comments. </p>";
	 }
	else {
	 $i = 1;

	 while($commrow = mysql_fetch_assoc($commresult)) {
	  echo "<a name='comment" . $i . "'>";
	  echo "<h3>Comment by " . $commrow['name'] . " on " . date("D jS F Y g.iA", 
	  strtotime($commrow['dateposted'])) . "</h3>";
	  echo $commrow['comment'];
	  $i++;
	 }
	}

	?>

	<h3>Leave a comment</h3>

	<form action="<?php echo $_SERVER['SCRIPT_NAME'] . "?id=" . $validentry; ?>" method="post">
	<table>
	<tr>
	  <td> Your name</td>
	  <td><input type="text" name="name"/></td>
	</tr>
	<tr>
	  <td>Comments</td>
	  <td><textarea name="comment" rows="10" cols="50"></textarea></td>
	</tr>
	<tr>
          <td><input type="submit" name="submit" value="Add comment"/></td>
	</tr>
	</table>
	</form>
<?php
    }
require("footer.php");
?>

 

Please enclose PHP code in

 . . . 

tags. It makes it much easier to read, and increases the chances that someone will take the time to help you.

 

EDIT: In the meantime, exactly what about it "doesn't work"?

 

 

 

<?php
require("config.php");

if(isset($_GET['id']) == TRUE) {
     if(is_numeric($_GET['id']) == FALSE) {
      $error = 1;
     }
     else
     {
      $error =0;  
      }
     if($error == 1 ) {
       header("Location: " . $config_basedir);
     }
     else {
      $validentry = $_GET['id'];
    }
}
else {
  $validentry = 0;
}

if(isset($_POST['submit'])){
               
        $db = mysql_connect($dbhost, $dbuser, $dbpassword);
        mysql_select_db($dbdatabase, $db);
        
        $sql = "INSERT INTO comments(blog_id, dataposted, name, comment) VALUES(" . 
            $validentry . ", NOW(), '" . $_POST['name'] . "', '" . $_POST['comment'] . "');";
      mysql_query($sql);
      
      header("Location: http://" . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . 
      "?id=" . $validentry);
}

else{

      require("header.php");
      
      if($validentry == 0) {
         $sql = "SELECT entries.*, categories.cat FROM  entries, categories " . 
         "WHERE entries.cat_id = categories.id " . 
         "ORDER BY dateposted DESC " .
         " LIMIT 1;";
      }
      else {
         $sql = "SELECT entries.*, categories.cat FROM  entries, categories " . 
         "WHERE entries.cat_id = categories.id AND entries.id = " . $validentry . 
         " ORDER BY dateposted DESC LIMIT 1;";
      }
      $result = mysql_query($sql);
      $row = mysql_fetch_assoc($result);
      echo "<h2>" . $row['subject'] . "</h2><br />";
      
      echo "<i>In <a href='viewcat.php?id=" . $row['cat_id'] . "'>" . $row['cat'] . 
      "</a> -  Posted on " . date("D jS F Y g.iA", strtotime($row['dateposted'])) . "</i>";
      
      echo "<p>";
      echo nl2br($row['body']);
      echo "</p>";
      
      $commsql = "SELECT * FROM  comments WHERE blog_id = " . $validentry . 
         " ORDER BY dateposted DESC;";
      $commresult = mysql_query($commsql);
      $numrows_comm = mysql_num_rows($commresult);
      
      if($numrows_comm ==0) {
         echo "<p> No comments. </p>";
       }
      else {
       $i = 1;
      
       while($commrow = mysql_fetch_assoc($commresult)) {
        echo "<a name='comment" . $i . "'>";
        echo "<h3>Comment by " . $commrow['name'] . " on " . date("D jS F Y g.iA", 
        strtotime($commrow['dateposted'])) . "</h3>";
        echo $commrow['comment'];
        $i++;
       }
      }
      
      ?>
      
      <h3>Leave a comment</h3>
      
      <form action="<?php echo $_SERVER['SCRIPT_NAME'] . "?id=" . $validentry; ?>" method="post">
      <table>
      <tr>
        <td> Your name</td>
        <td><input type="text" name="name"/></td>
      </tr>
      <tr>
        <td>Comments</td>
        <td><textarea name="comment" rows="10" cols="50"></textarea></td>
      </tr>
      <tr>
          <td><input type="submit" name="submit" value="Add comment"/></td>
      </tr>
      </table>
      </form>
<?php
    }
require("footer.php");
?>

When i click on the submit button, The if(isset($_POST['submit'])) is always not set, so the code will not go into the if statement and directly go to the else statement.

Please help me!!

 

 

All my friends,

 

the  if(isset($_POST['submit'])) is not work, no matter what i have tried.

it seems very simple problem, but i can't get a clue to solve it.

 

Please help me !!!!!!! thank you in advance!!!!!!!!!

 

 


<?php
require("config.php");

if(isset($_GET['id']) == TRUE) {
  if(is_numeric($_GET['id']) == FALSE) {
   $error = 1;
  }
  else
  {
	$error =0;  
   }
  if($error == 1 ) {
	 header("Location: " . $config_basedir);
  }
  else {
	$validentry = $_GET['id'];
 }
}
else {
  $validentry = 0;
}

if(isset($_POST['submit'])){

	  $db = mysql_connect($dbhost, $dbuser, $dbpassword);
	  mysql_select_db($dbdatabase, $db);
	  
	  $sql = "INSERT INTO comments(blog_id, dataposted, name, comment) VALUES(" . 
			$validentry . ", NOW(), '" . $_POST['name'] . "', '" . $_POST['comment'] . "');";
	mysql_query($sql);

	header("Location: http://" . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . 
	"?id=" . $validentry);
}

else{

	require("header.php");

	if($validentry == 0) {
		$sql = "SELECT entries.*, categories.cat FROM  entries, categories " . 
		"WHERE entries.cat_id = categories.id " . 
		"ORDER BY dateposted DESC " .
		" LIMIT 1;";
	}
	else {
		$sql = "SELECT entries.*, categories.cat FROM  entries, categories " . 
		"WHERE entries.cat_id = categories.id AND entries.id = " . $validentry . 
		" ORDER BY dateposted DESC LIMIT 1;";
	}
	$result = mysql_query($sql);
	$row = mysql_fetch_assoc($result);
	echo "<h2>" . $row['subject'] . "</h2><br />";

	echo "<i>In <a href='viewcat.php?id=" . $row['cat_id'] . "'>" . $row['cat'] . 
	"</a> -  Posted on " . date("D jS F Y g.iA", strtotime($row['dateposted'])) . "</i>";

	echo "<p>";
	echo nl2br($row['body']);
	echo "</p>";

	$commsql = "SELECT * FROM  comments WHERE blog_id = " . $validentry . 
		" ORDER BY dateposted DESC;";
	$commresult = mysql_query($commsql);
	$numrows_comm = mysql_num_rows($commresult);

	if($numrows_comm ==0) {
	   echo "<p> No comments. </p>";
	 }
	else {
	 $i = 1;

	 while($commrow = mysql_fetch_assoc($commresult)) {
	  echo "<a name='comment" . $i . "'>";
	  echo "<h3>Comment by " . $commrow['name'] . " on " . date("D jS F Y g.iA", 
	  strtotime($commrow['dateposted'])) . "</h3>";
	  echo $commrow['comment'];
	  $i++;
	 }
	}

	?>

	<h3>Leave a comment</h3>

	<form action="<?php echo $_SERVER['SCRIPT_NAME'] . "?id=" . $validentry; ?>" method="post">
	<table>
	<tr>
	  <td> Your name</td>
	  <td><input type="text" name="name"/></td>
	</tr>
	<tr>
	  <td>Comments</td>
	  <td><textarea name="comment" rows="10" cols="50"></textarea></td>
	</tr>
	<tr>
          <td><input type="submit" name="submit" value="Add comment"/></td>
	</tr>
	</table>
	</form>
<?php
    }
require("footer.php");
?>

Thank you for your reply, I have tried this, but got an error:

 

Notice:  Undefined index: submit in  on line 26

 

 

 

 

<?php
if ($_POST['submit'] == ''Add comment") {

}
?>

Also your Xhtml and PHP is malformed. Run the Xhtml through a validator. That might explain why it's being ignored instead of passing the variables.

Proper form is important.

It works fine for me locally. Add the line like I did below and post what happens.

 

if(isset($_POST['submit'])){

	echo "POST is set";   //  < ---- ADD THIS LINE		

	  $db = mysql_connect($dbhost, $dbuser, $dbpassword);
	  mysql_select_db($dbdatabase, $db);

Thank you Pikachu2000:

 

I have test as you said before, here is the code:

 

if(isset($_POST['submit'])){

 

echo "runing here!!!!!!!!!!!!!!!!!!";

 

  $db = mysql_connect($dbhost, $dbuser, $dbpassword);

  mysql_select_db($dbdatabase, $db);

 

Nothing happen! 

 

something wrong with my environment??  php, mysql and apache are all the newest version!

 

Thank you again!

 

 

It works fine for me locally. Add the line like I did below and post what happens.

 

if(isset($_POST['submit'])){

	echo "POST is set";   //  < ---- ADD THIS LINE		

	  $db = mysql_connect($dbhost, $dbuser, $dbpassword);
	  mysql_select_db($dbdatabase, $db);

What exactly happens? Blank screen or what?  Try the same thing with the else statement.

 

else{

 

echo "Goes to the ELSE statement"; // Add debugging echo.

 

      require("header.php");

here is what i got: the _GET works fine.

but the  _POST is not work

 

<pre>$_GET: Array

(

    [id] => 1

)

</pre><pre>$_POST: Array

(

)

</pre>

 

Thank you!!

 

At the start of your script add

<?php
echo '<pre>$_GET: ' . print_r($_GET,true) . '</pre>';
echo '<pre>$_POST: ' . print_r($_POST,true) . '</pre>';
?>

What is printed when you submit your form?

 

Ken

Yes, when i clicked on the "add comment" button,  it go direct to the else statement, i got: Goes to the ELSE statement

 

Thank you!

 

What exactly happens? Blank screen or what?  Try the same thing with the else statement.

 

else{

 

echo "Goes to the ELSE statement"; // Add debugging echo.

 

      require("header.php");

Do you have a different web browser you can use to try it? Maybe Firefox, or Opera? I'm starting to think the browser is choking because the script uses both $_POST and $_GET, and it worked fine here for me with Safari.

 

I have tried Firofox and IE, both are not work.

Everything works great, just the $_POST here is not work.

I doubt something wrong with the wampserver. or some configuration is not ok?

Thank you for your help!

 

 

Do you have a different web browser you can use to try it? Maybe Firefox, or Opera? I'm starting to think the browser is choking because the script uses both $_POST and $_GET, and it worked fine here for me with Safari.

Try this simple script and tell us what you get:

<?php
echo '<pre>$_GET: ' . print_r($_GET,true) . '</pre>';
echo '<pre>$_POST: ' . print_r($_POST,true) . '</pre>';
?>
<html>
<head>
	<title>Test Get Post</title>
</head>
<body>
	<form action="?item=this+is+a+test" method="post">
		<p>
			Please enter somthing: <input type="text" name="testing"><br>
			Comments: <textarea name="comments"></textarea><br>
			<input type="submit" name="submit" value="Test Form">
		</p>
	</form>
</body>
</html>

 

This will tell us whether it's the server set up or your code.

 

When I run this script on my local server, I get:

$_GET: Array
(
    [item] => this is a test
)
$_POST: Array
(
    [testing] => test
    [comments] => restetrt
    [submit] => Test Form
)

 

Ken

Ken, Thank you for your time!

 

I test your code, here is what i have got:

it seems working greatly!

 

$_GET: Array

(

    [item] => this is a test

)

 

$_POST: Array

(

    [testing] => test

    [comments] => testttt

    [submit] => Test Form

)

 

 

 

 

 

Try this simple script and tell us what you get:

<?php
echo '<pre>$_GET: ' . print_r($_GET,true) . '</pre>';
echo '<pre>$_POST: ' . print_r($_POST,true) . '</pre>';
?>
<html>
<head>
	<title>Test Get Post</title>
</head>
<body>
	<form action="?item=this+is+a+test" method="post">
		<p>
			Please enter somthing: <input type="text" name="testing"><br>
			Comments: <textarea name="comments"></textarea><br>
			<input type="submit" name="submit" value="Test Form">
		</p>
	</form>
</body>
</html>

 

This will tell us whether it's the server set up or your code.

 

When I run this script on my local server, I get:

$_GET: Array
(
    [item] => this is a test
)
$_POST: Array
(
    [testing] => test
    [comments] => restetrt
    [submit] => Test Form
)

 

Ken

Paste this script into a new file, run it, and see what happens. If it works, there's a problem somewhere in the one you've been using. It could be a bad linefeed, or a control character, or even a word misspelled. If not, well, we'll cross that bridge if we get to it.

 

<?php
if( isset($_GET['get']) ) {
echo "GET vars are: ";
print_r($_GET);
echo "<br /><br />";
}
if( isset($_POST['submit']) ) {
echo '$_POST[\'submit\'] is SET.<br />';
print_r($_POST);
echo "<br /><br />";
}
if( isset($_POST['hidden']) ) {
echo '$_POST[\'hidden\'] is SET.';
} else {
?>

<form action="?get=GetVarsAreSet" method="POST">
Enter a value here:<input type="text" name="one"><br />
Enter a different value:<input type="text" name="two" /><br />
<input type="hidden" name="hidden" value="hidden" />
<input type="submit" name="submit" value="Submit" />
</form>

<?php
}
?>

 

 

Ken, Thank you so much for your help!

 

I am looking forward to your reply!

 

Thank you!

 

Ok, so the Post is working. I will try to take a closer look at your code tomorrow (actually later today, since it's almost 2 am here... and I really have to get to sleep. :) )

 

 

 

 

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.