Jump to content

Recommended Posts

I want to make a form with two radio buttons.

Sort: A-Z [button] Z-A [button];

 

and in the same page I wanna connect to my database and let see the cols DESC or ASC?

 

I was thinking of something like this:

<form action='gedichten.php' method='POST'>
			From Z To A: <input type="radio" name="ZA" value="ZA" CHECKED>
			From A To Z: <input type="radio" name="AZ" value="AZ">
		</form>
                    <?php
require_once 'config.php';                // our database settings
$conn = mysql_connect($dbhost,$dbuser,$dbpass)
        or die('Error connecting to mysql');
mysql_select_db($dbname);
if($_POST['ZA']) {
	$query = sprintf("SELECT PName,UName,PText,URL,category From gedichten ORDER BY PName DESC");
	$result = mysql_query($query);
	while($urlgedicht = mysql_fetch_array($result)) {
		print "<ul><li><a href={$urlgedicht['category']}/{$urlgedicht['URL']}.htm>{$urlgedicht['PName']} | {$urlgedicht['UName']} | {$urlgedicht['category']}</a></li></ul>";
	}
} elseif($_POST['AZ']) {
	$query = sprintf("SELECT PName,UName,PText,URL,category From gedichten ORDER BY PName ASC");
	$result = mysql_query($query);
	while($urlgedicht = mysql_fetch_array($result)) {
		print "<ul><li><a href={$urlgedicht['category']}/{$urlgedicht['URL']}.htm>{$urlgedicht['PName']} | {$urlgedicht['UName']} | {$urlgedicht['category']}</a></li></ul>";
	}
} else {
	print "<script type='text/javascript'>alert('There was an error while trying to give an overview!');</script>";
}
?>

,

 

but it doesn't work at all.

 

Link to comment
https://forums.phpfreaks.com/topic/143367-solved-desc-asc/
Share on other sites

All radio buttons referring to a single option must have the same name, only values should be different...

 

A quick example...

 

<form action='gedichten.php' method='post'>
From Z To A: <input type="radio" name="order" value="ZA" CHECKED>
From A To Z: <input type="radio" name="order" value="AZ">
</form>
<?php

$order  = 'asc';

require_once 'config.php';

$conn = mysql_connect ( $dbhost,$dbuser,$dbpass ) or die ( 'Connection Error: ' . mysql_error () );

mysql_select_db ( $dbname ) or die ( 'Select Database Error: ' . mysql_error () );

if ( isset ( $_POST['order'] ) )
{
if ( $_POST['order'] == 'ZA' )
{
	$order = 'desc';
}
else if ( $_POST['order'] == 'AZ' )
{
	$order = 'asc';
}
}

$result = mysql_query ( "SELECT PName, UName, PText, URL, category From gedichten ORDER BY PName " . $order . ";" ) or die ( 'Query Error: ' . mysql_error () );

if ( mysql_num_rows ( $result ) == 0 )
{
echo "<script type='text/javascript'>
	alert('There was an error while trying to give an overview!');
</script>
";
}
else
{
while ( $urlgedicht = mysql_fetch_assoc ( $result ) )
{
	echo "	<ul>
	<li>
		<a href='" . $urlgedicht['category'] . "/" . $urlgedicht['URL'] . ".htm'>" . $urlgedicht['PName'] . " | " . $urlgedicht['UName'] . " | " . $urlgedicht['category'] . "</a>
	</li>
</ul>
";
}
}

?>

 

 

Next time don't double post, wait for someone to answer you and if they don't after a day, bump your post then!

Link to comment
https://forums.phpfreaks.com/topic/143367-solved-desc-asc/#findComment-751915
Share on other sites

well I'll take you word for it...it probably doesn't work, but you'd get better results if you changed your code to this

</pre>
<form action="'gedichten.php'" method="'POST'">
            From Z To A: 
            From A To Z: 
</form>
<br>                    require_once 'config.php';                // our database settings<br>$conn = mysql_connect($dbhost,$dbuser,$dbpass)<br>        or die('Error connecting to mysql');<br>mysql_select_db($dbname);<br>   if(isset($_POST['sort'])) {<br>      $query = sprintf("SELECT PName,UName,PText,URL,category From gedichten ORDER BY PName" . $_POST['sort']);<br>      $rresult = mysql_query($query);<br>      while($urlgedicht = mysql_fetch_array($result))<br>         print "<ul>{$urlgedicht['PName']} | {$urlgedicht['UName']} | {$urlgedicht['category']}</ul>";<br>   } else {<br>      print "<script type="'text/javascript'">alert(&#039;There was an error while trying to give an overview!&#039;);</script>";<br>   }<br>?&g

related radio buttons need to all have the same name

Link to comment
https://forums.phpfreaks.com/topic/143367-solved-desc-asc/#findComment-751917
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.