Jump to content


Photo

Conditional Header redirect not working...


Best Answer Jim R, 15 January 2018 - 10:53 AM

I figured out there was an Ajax setting blocking the redirect with the form plugin I'm using.  No clue why it would do that, but by turning it off it took care of my problem.  

 

As always, guys, thank you for your time and help.  

Go to the full post


  • Please log in to reply
9 replies to this topic

#1 Jim R

Jim R
  • Members
  • PipPipPip
  • Advanced Member
  • 576 posts

Posted 13 January 2018 - 03:57 PM

A form posts to a_players_reviews directly.  It then pushes information to this code below to check if a corresponding Row exists in a_players.  If it doesn't, I want to redirect it to page where my User can add more information in a second form.  

 

I know this works down to the if (($num_rows) == 0) {      because I got some help here in rearranging the functions, and at that time it was to enter a Row into a_players itself.  However, now I just want to redirect to a page with a second form on it.  

 

When I enter a name that already exists, the form works as it should.  When I enter a name that doesn't exist, triggering the IF, I get an AJAX error.  I think I have the Header syntax correct, but this is the first time I've tried this.  

     $data = array(
        'f_school' => $form->getValue('quform_1_7'),
        'f_grade' => $form->getValue('quform_1_5'),
        'f_nameFirst' => $form->getValue('quform_1_3'),
        'f_nameLast' => $form->getValue('quform_1_4')
    );
  			
 		$nameFirst = $data['f_nameFirst'];
 		$nameLast = $data['f_nameLast'];
 		$grade = $data['f_grade'];
 		

include (ABSPATH ."resources/connection.php");

 
$query = "SELECT nameFirst, nameLast, grade
		FROM a_players
		WHERE nameFirst = '{$data['f_nameFirst']}' 
		AND nameLast = '{$data['f_nameLast']}' 
		AND grade = '{$data['f_grade']}'";

$results = mysql_query($query);

$num_rows = mysql_num_rows($results);


		if (($num_rows) == 0) {		
			header("Location: /ccoaches/player-input"); /* Redirect browser */
			exit;
		}



 



#2 NotionCommotion

NotionCommotion
  • Members
  • PipPipPip
  • Advanced Member
  • 1,511 posts

Posted 13 January 2018 - 04:07 PM

What ajax error do you get?  Use chrome, etc debugger to look at your post response.  Or better yet, enable php error reporting, and check what that says.

 

Note that you will get an error if the server sends any content to the client before the header.  This can even be an empty space.  Check if include (ABSPATH ."resources/connection.php"); might be doing so.  Note the following will result in an error:

<?php
include (ABSPATH ."resources/connection.php");
header("Location: /ccoaches/player-input"); /* Redirect browser */
<?php
//bla bla bla
?>

Why?  Because there is a space after the included files closing php tag.  Such is a reason why many will not use the final closing tag.


Edited by NotionCommotion, 13 January 2018 - 04:10 PM.

NotionCommotion

#3 Jim R

Jim R
  • Members
  • PipPipPip
  • Advanced Member
  • 576 posts

Posted 13 January 2018 - 04:46 PM

Here is connection.php

<?php



$con = mysql_connect("localhost","username","password");
mysql_select_db("table", $con);

?>


#4 NotionCommotion

NotionCommotion
  • Members
  • PipPipPip
  • Advanced Member
  • 1,511 posts

Posted 13 January 2018 - 04:57 PM

Is there any chance that there is an empty line before your opening php tag or after your closing php tag?  What does PHP say is the problem?


NotionCommotion

#5 Jim R

Jim R
  • Members
  • PipPipPip
  • Advanced Member
  • 576 posts

Posted 13 January 2018 - 05:43 PM

There are no line before or after the php tags.  Not sure about enabling error reporting.  Where does that go?  I have a log file set up.  Should I send it to that?  

 

 

 

I'm using Safari.  



#6 dodgeitorelse3

dodgeitorelse3
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationMassena, NY

Posted 13 January 2018 - 06:02 PM

try replacing

 

if (($num_rows) == 0)

 

with

 

if ($num_rows == 0)



#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 17,973 posts

Posted 13 January 2018 - 06:34 PM

Not sure about enabling error reporting.  Where does that go?  


In your php.ini file. Set the error reporting level to E_ALL. The usual convention is to display errors when developing and log errors on the production server.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 Jim R

Jim R
  • Members
  • PipPipPip
  • Advanced Member
  • 576 posts

Posted 13 January 2018 - 06:59 PM

try replacing

 

if (($num_rows) == 0)

 

with

 

if ($num_rows == 0)

 

The IF loop isn't part of the problem.  It's already proven to work, and even in this instance it's working.  



#9 Jim R

Jim R
  • Members
  • PipPipPip
  • Advanced Member
  • 576 posts

Posted 13 January 2018 - 07:01 PM

In your php.ini file. Set the error reporting level to E_ALL. The usual convention is to display errors when developing and log errors on the production server.

 

It already is. 

 

I doubt I'm doing much in a conventional manner.   :-\


Edited by Jim R, 13 January 2018 - 07:09 PM.


#10 Jim R

Jim R
  • Members
  • PipPipPip
  • Advanced Member
  • 576 posts

Posted 15 January 2018 - 10:53 AM   Best Answer

I figured out there was an Ajax setting blocking the redirect with the form plugin I'm using.  No clue why it would do that, but by turning it off it took care of my problem.  

 

As always, guys, thank you for your time and help.  






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users