4 Replies - 943 Views - Last Post: 20 October 2013 - 10:14 AM

#1 boba fett  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 18-August 13

Not getting to next page when press button

Posted 19 October 2013 - 05:47 PM

I have a small website with a blog. The pages php and I use php and mysql to interact with the database. If someone is logged in they can post comments in the blog. I recently added a javascript to validate users input and since I did, the posts don't work any more. The java script just simply checks to make sure that all the input boxes are filled out and it works telling the user if they left one of the boxes blank. But its not getting to the next page that handles the form. There are 3 boxes to fill out: category(a text box), title(a text box) and content(a textarea).

Here is the code for the main page with the form(index.php)>
This is up in the header>> <script src="post.js"></script>
This is in the body>>
<?php
//show blog
echo '<table border="1">';
echo '<tr>';
echo '<th>Category</th>';
echo '<th>Title</th>';
echo '<th>Content</th>';
echo '</tr>';
$result = mysqli_query($con,"SELECT * FROM blog ORDER BY id");
while($row = mysqli_fetch_array($result)) {
	echo '<tr>';      					
	echo '<td>' . $row['category'] . '</td>';      						
	echo '<td>' . $row['title'] . '</td>';						
	echo '<td>' . $row['content'] . '</td>';							
	echo '</tr>';      					      											
}
echo '</table>';
?>										
<form name="post1" action="post.php" method="post">
Category: <input type="text" name="category" /><br>
Title: <input type="text" name="title" /><br>
Content: <textarea name="content"></textarea><br>							
<input type="button" value="Post" onclick="post()" />
</form>


Here is the code for the javascript(post.js)>
function post() {
	//see if category filled out  
	var cat=document.post1.category.value;
	if (cat===null || cat==="") {
    	   alert("Category must be filled out");
     	   return;
  	}
	//see if title filled out
  	var tit=document.post1.title.value;
  	if (tit===null || tit==="") {
  	   alert("Title must be filled out");
     	return;
  	}
	//see if content filled out
  	var con=document.post1.content.value;
  	if (con===null || con==="") {
     	   alert("Content must be filled out");
     	   return;
  	}
  	form.submit();
}


And here is the code for the process page(post.php)>
<?php
include '../system/db_connect.php';
include '../system/functions.php';
sec_session_start(); 
if(login_check($con) !== true) {//see if user is logged in
	echo 'you must be logged in to post';
	die();								
}
if(isset($_POST['category'], $_POST['title'], $_POST['content'])) { 
   $category = $_POST['category'];
   $title = $_POST['title'];
   $content = $_POST['content'];
}else { 
   echo 'Invalid Request';
   die();
}
if ($insert_stmt = $con->prepare("INSERT INTO blog (category, title, content) VALUES (?, ?, ?)")) {    
  $insert_stmt->bind_param('sss', $category, $title, $content); 
  $insert_stmt->execute();
}
header('Location: index.php');
?>


I have no idea why the form is not processing now. I tried changing the button type to submit from button and it gets to and executes the post.php page but the javascript validation doesn't work then. Any advice would be greatly appreciated, thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Not getting to next page when press button

#2 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,910
  • Joined: 12-December 12

Re: Not getting to next page when press button

Posted 19 October 2013 - 07:25 PM

form.submit();

This is incorrect and possibly creates an error in the Javascript. You might change it to:

document.post1.submit();

Better would be to use a normal submit button and the onsubmit attribute for the form. In the function that this calls either return true; or false depending on whether the form should continue to be submitted.

Also check your browser's console for any errors.

This post has been edited by andrewsw: 19 October 2013 - 07:26 PM

Was This Post Helpful? 1
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: Not getting to next page when press button

Posted 20 October 2013 - 12:38 AM

additional note: a form elementís value never ever returns null. and besides that, null == '' // true
Was This Post Helpful? 0
  • +
  • -

#4 boba fett  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 18-August 13

Re: Not getting to next page when press button

Posted 20 October 2013 - 10:08 AM

Ok I changed the button/input type to "submit" and changed onclick to onsubmit to trigger the javascript function and it goes to the post page but doesn't do the javascript now. :(/> I absolutely know because I put in a test alert box at beginning of function.

I also added false at the end of all the returns if it catches a blank text box and return true at end of function.

I also changed if (cat===null) || (cat==="") to just if (cat=="")

In a way it's fixed I guess, but its like there isn't any javascript now, so no validation.
Was This Post Helpful? 0
  • +
  • -

#5 boba fett  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 18-August 13

Re: Not getting to next page when press button

Posted 20 October 2013 - 10:14 AM

Ok, I went with PLAN B. I changed the button in the form back to "button" from submit, changed onsubmit back to onclick and changed the end of the JS function to say document.post1.submit(); and it works! Yay

Thanks for the help guys. :)/>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1