MJXShipton Posted August 6, 2009 Share Posted August 6, 2009 Head's up - I'm a beginner at PHP but fairly intermediate at programming in general. So I'm working through this great book I got, "PHP and MySQL with practical applications," or something like that. Right now I'm building a blog with it. All was going well till I added the "add comment" feature which uses a form at the bottom of the page. I think that the error lies in the <form method="post" action="<?php echo $_SERVER['SCRIPT_NAME'] . "?id=" . $validentry; ?>"> portion. When I enter info, and press 'submit,' Chrome does nothing, IE gives the page not available error page. But then if I return to the appropriate page (say id=1), the comment has been added. I've tried hard coding the 'action' portion in, no avail - but if I change it to www.google.com, for instance, it redirects me to that page successfully (with associated error regarding disallowance of the post method). I'm absolutely stumped. Any help would be greatly appreciated. Full code for viewentry.php: <?php require("config.php"); if(isset($_GET['id']) == TRUE) { if(is_numeric($_GET['id']) == FALSE) { $error = 1; } if($error == 1) { header('Location: http://127.0.0.1'); } else { $validentry = $_GET['id']; } } else { $validentry = 0; } if($_POST['submit']) { $db = mysql_connect($dbhost, $dbuser, $dbpassword); mysql_select_db($dbdatabase, $db); $sql = "INSERT INTO comments(blog_id, dateposted, name, comment) VALUES(". $validentry . ", NOW(),'" . $_POST['name'] . "','" . $_POST['comment'] . "');"; mysql_query($sql); header("Location: http://" . $HTTP_HOST . $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 = " . $row['id'] . " ORDER BY dateposted;"; $commresult = mysql_query($commsql); $numrows_comm = mysql_num_rows($commresult); if($numrows_comm == 0) { echo "<p>No comments.</p>"; } else { echo "(<strong>" . $numrows_comm . "</strong>) comments : <br>" ; $i = 1; while($commrow = mysql_fetch_assoc($commresult)) { echo "<a name='comment" . $i . "'>"; echo "<i>Comment by " . $commrow['name'] . " on " . date("D jS F Y g. iA", strtotime($commrow['dateposted'])) . "</i><br />"; echo "<p>"; echo "<table><tr><td>"; echo $commrow['comment']; echo "</td></tr></table>"; echo "</p>"; $i++; } } echo $SCRIPT_NAME; ?> <h3>Leave a Comment</h3> <form method="post" action="<?php echo $_SERVER['SCRIPT_NAME'] . "?id=" . $validentry; ?>"> <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></td> <td><input type="submit" name="submit" value="Add Comment"></td> </tr> </table> </form> <?php require("footer.php"); ?> Link to comment https://forums.phpfreaks.com/topic/169082-php-form-submission-problem-likely-action/ Share on other sites More sharing options...
watsmyname Posted August 6, 2009 Share Posted August 6, 2009 You are trying to submit form to same page right? try this action="<?php echo $_SERVER['PHP_SELF']."?id=$validentry"; ?>" Link to comment https://forums.phpfreaks.com/topic/169082-php-form-submission-problem-likely-action/#findComment-892081 Share on other sites More sharing options...
MJXShipton Posted August 6, 2009 Author Share Posted August 6, 2009 No go. Exact same thing. When I check the source, it seems right; <h3>Leave a Comment</h3> <form method="post" action="/viewentry.php?id=1"> <table> <tr> So the php worked. But when I click the submit button, say in Chrome, nothing happens; reload, and the comment appears, so it clearly did the mysql submission. Link to comment https://forums.phpfreaks.com/topic/169082-php-form-submission-problem-likely-action/#findComment-892084 Share on other sites More sharing options...
MJXShipton Posted August 6, 2009 Author Share Posted August 6, 2009 Got it. The problem was in the header() command in the processing part of the header. The book fails to use the $_SERVER variables properly - namely by omitting the $_SERVER part. So $HTTP_HOST and $SCRIPT_NAME had no values. Link to comment https://forums.phpfreaks.com/topic/169082-php-form-submission-problem-likely-action/#findComment-892090 Share on other sites More sharing options...
_DarkLink_ Posted August 6, 2009 Share Posted August 6, 2009 Hello, Is it just me or are you ending the else statement as soon as you started it? <?php if($_POST['submit']) { $db = mysql_connect($dbhost, $dbuser, $dbpassword); mysql_select_db($dbdatabase, $db); $sql = "INSERT INTO comments(blog_id, dateposted, name, comment) VALUES(". $validentry . ", NOW(),'" . $_POST['name'] . "','" . $_POST['comment'] . "');"; mysql_query($sql); header("Location: http://" . $HTTP_HOST . $SCRIPT_NAME . "?id=" . $validentry); } else { }//Here... ?> Try putting the ending curly bracket at the end of everything, after the closing HTML tags for example. Such as here: <?php require("footer.php"); }//Close the else statement. ?> Hope it helps. Link to comment https://forums.phpfreaks.com/topic/169082-php-form-submission-problem-likely-action/#findComment-892093 Share on other sites More sharing options...
watsmyname Posted August 6, 2009 Share Posted August 6, 2009 Hello, Is it just me or are you ending the else statement as soon as you started it? <?php if($_POST['submit']) { $db = mysql_connect($dbhost, $dbuser, $dbpassword); mysql_select_db($dbdatabase, $db); $sql = "INSERT INTO comments(blog_id, dateposted, name, comment) VALUES(". $validentry . ", NOW(),'" . $_POST['name'] . "','" . $_POST['comment'] . "');"; mysql_query($sql); header("Location: http://" . $HTTP_HOST . $SCRIPT_NAME . "?id=" . $validentry); } else { }//Here... ?> Try putting the ending curly bracket at the end of everything, after the closing HTML tags for example. Such as here: <?php require("footer.php"); }//Close the else statement. ?> Hope it helps. well nothing wrong with his code, its not necessary you need to write something inside else condition but he should have omitted else part, in current scenerio its not necessary. Link to comment https://forums.phpfreaks.com/topic/169082-php-form-submission-problem-likely-action/#findComment-892098 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.