Jump to content

[SOLVED] Hidden field problem


Andy17

Recommended Posts

Hey there!

 

I am not 100% sure, but I believe my problem is that my variable $number does not get passed correctly by my hidden field. My problem is that after clicking the Next button, the number does not increment like I want it to (notice my check below the button).

 

Here is the script in action:

http://menu.jokeheaven.eu/randomjoke.php

 

 

And here is the code:

 

<?php

// Establishing MySQL connection
mysql_connect("db_server", "username", "password") or die(mysql_error());


// Selecting database
mysql_select_db("db_name") or die(mysql_error());


// Finding max number of rows
$sql2 = "SELECT submitter FROM jokes";
$result2 = mysql_query($sql2);

if ($result2)

{

	$maxrows = mysql_num_rows($result2);

}



function next_button()

{

$number++;


// Finding the row with the number as id - if it exists
$sql1 = "SELECT * FROM jokes WHERE id = '$number'";
$result = mysql_query($sql1);


if ($result)

{

$count = mysql_num_rows($result);


if ($count == 1)

	{

		$row = mysql_fetch_array($result);

		$submitter = $row['submitter'];
		$title = $row['title'];
		$joke = $row['joke'];

		echo "<p><b>Submitter:</b> " . $submitter . "<br><br><b>Title:</b> " . $title . "<br><br>" . $joke . "</p>";

		echo "<br><br>" . $number;

	}


}


else

{

echo "error";

}

}


function previous_button()

{

$number--;

// Finding the row with the number as id - if it exists
$sql1 = "SELECT * FROM jokes WHERE id = '$number'";
$result = mysql_query($sql1);


if ($result)

{

$count = mysql_num_rows($result);


if ($count == 1)

	{

		$row = mysql_fetch_array($result);

		$submitter = $row['submitter'];
		$title = $row['title'];
		$joke = $row['joke'];

		echo "<b>Submitter:</b> " . $submitter . "<br><br><b>Title:</b> " . $title . "<br><br>" . $joke;

		echo "<br><br>" . $number;

	}


}


else

{

echo "error";

}

}



if (isset($_POST['next']))

{

	next_button();

}

elseif (isset($_POST['previous']))

{

	previous_button();

}

else

{

	if(isset($number))

		{

			echo "this is just a test";

		}

	else

		{

                                // If the variable has not been set yet, it's set to 0 and some text is displayed
			$number = 0;

			echo "<b>Submitter:</b> Zeppy<br><br><b>Title:</b> Blonde in a Boat<br><br><br>There was a blonde driving down the road one day. She glanced to her right and noticed another blonde sitting in a nearby field, rowing a boat with no water in sight. The blonde angrily pulled her car over and yelled at the rowing blonde, \"What do you think you're doing? It's things like this that give us blondes a bad name. If I could swim, I'd come out there and kick your butt!\"<br><br>";

		}

}

?>



<form action="<?= $php_SELF ?>" method="post">

<?php

if ($number > 0)

{

?>

<input type="submit" name="previous" value="Previous">

<?php

}

if ($number < $maxrows)

{

?>

<input type="submit" name="next" value="Next">

<input type="hidden" name="number" value="<?php echo $number; ?>"/>

<input type="hidden" name="rows" value="<?php echo $maxrows; ?>"/>

</form>

<?php

}

echo "<br><br>Number: " . $number . "<br> Rows: " . $maxrows;

?>

 

Basically, what is happening is that when clicking a button, it calls either one of the functions. In those functions, $number will be incremented or decremented, but I believe the problem is that the updated variable is not passed on when clicking either one of the buttons - or my placement of the $number++ and $number-- is wrong. Any ideas?

Link to comment
Share on other sites

Yeah sorry about the messy code, I'm still new to PHP. I cleaned up the code a bit and put $number into the function parameter. Now it will let me go forward by pressing the next button, but when I try to go back by pressing the previous button, it cannot find the variable $number anymore.

 

<?php

// Establishing MySQL connection
mysql_connect("db_server", "username", "password") or die(mysql_error());


// Selecting database
mysql_select_db("db_name") or die(mysql_error());


// Finding all rows
$sql2 = "SELECT submitter FROM jokes";
$result2 = mysql_query($sql2);

if ($result2)

{

	$maxrows = mysql_num_rows($result2);

}


function button1($number)

{

// Finding the row with the number as id - if it exists
$sql1 = "SELECT * FROM jokes WHERE id = '$number'";
$result = mysql_query($sql1);


if ($result)

{

$count = mysql_num_rows($result);


if ($count == 1)

	{

		$row = mysql_fetch_array($result);

		$submitter = $row['submitter'];
		$title = $row['title'];
		$joke = $row['joke'];

		echo "<b>Submitter:</b> " . $submitter . "<br><br><b>Title:</b> " . $title . "<br><br>" . $joke . "<br><br>" . $number;

	}


}


else

{

echo "error";

}

}


// Checking which button the user pressed
if (isset($_POST['next']))

{

	$number++;
	button1($number);

}

elseif (isset($_POST['previous']))

{

	$number--;
	button1($number);

}

else

{

	if(isset($number))

		{

			echo "just testing!";

		}

	else

		{

			$number = 0;

			echo "<b>Submitter:</b> Zeppy<br><br><b>Title:</b> Blonde in a Boat<br><br><br>There was a blonde driving down the road one 

day. She glanced to her right and noticed another blonde sitting in a nearby field, rowing a boat with no water in sight. The blonde angrily pulled her car 

over and yelled at the rowing blonde, \"What do you think you're doing? It's things like this that give us blondes a bad name. If I could swim, I'd come out 

there and kick your butt!\"<br><br>";

		}

}

?>


<form action="<?= $php_SELF ?>" method="post">

<?php

if ($number > 0)

{

?>

<input type="submit" name="previous" value="Previous">

<?php

}

if ($number < $maxrows)

{

?>

<input type="submit" name="next" value="Next">

<input type="hidden" name="number" value="<?php echo $number; ?>"/>

<input type="hidden" name="rows" value="<?php echo $maxrows; ?>"/>

</form>

<?php

}

echo "<br><br>Number: " . $number . "<br> Rows: " . $maxrows;

?>

 

Code in action:

http://menu.jokeheaven.eu/randomjoke.php

 

Any ideas what I did wrong / what I should do?

 

Thanks for the help so far!

 

 

Why not use a LIMIT on your query instead of trying to find the next "row" through a query let the LIMITS do it

 

I'm new to PHP and MySQL so I didn't even know of that yet. Anyways, it's quite simple, but the reason why I used "WHERE id = '$number'" is that it seemed easier to go to the next/previous row when clicking a button. Any thought of how you would do that when using LIMIT?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.