Forum Moderators: coopster

Message Too Old, No Replies

Problem using $ GET

Switching from $_POST To $_GET

         

dbarasuk

5:44 pm on Dec 14, 2009 (gmt 0)

10+ Year Member



Hello,
i have a PHP program designed to search a thesis database to see if a thesis on which a student would like to work on has not been done by someone else in the past. In the beginning a form is provided where the student can type some keyword in and hit the submit button to see if there is any related thesis.

Up to that level, there is any problem because the program can retrieve a couple of related thesis with a good pagination system.

The problem however happens when i start clicking the "Next" and "Previous" links because the program returns nothing once i click those pagination links.

Here is the code. Please ignore the html part of the code because it is french, do consider only the PHP, code. If someone has a suggestion that can help navigate the different resultsets, i will be very grateful.

<?php
// inclure le fichier de connection
$connection = mysqli_connect("localhost", "ulbu", "ulbu", "annee2003_2004");

// Initialiser le nombre max de lignes à afficher

define("MAXSHOW", 5);
// Déterminer ce qui se passera si le button "submit" est cliqué

$motcle = isset($_POST['motcle'])? mysqli_real_escape_string($connection, strip_tags(trim($_POST["motcle"]))):"";
$faculte = isset($_POST['faculte'])? mysqli_real_escape_string($connection, strip_tags(trim($_POST["faculte"]))):"";

// Préparer la requete
$sql = "SELECT COUNT(*) FROM memoires WHERE sujet RLIKE '$motcle' AND faculte ='$faculte'";
// Envoyer requête
$result = mysqli_query($connection, $sql);

if($result)
{
$row = mysqli_fetch_row($result);
$totals = $row[0];
}

else{
$just_loaded = "<span>La page vient juste de se charger</<span>";
}

if(isset($totals) and $totals > 0)
$page_courante = isset($_GET["page_courante"])?$_GET["page_courante"]:0;
$numero_premiereligne = $page_courante*MAXSHOW;
$sql1 = "SELECT * FROM memoires WHERE sujet RLIKE '$motcle' AND faculte ='$faculte' LIMIT $numero_premiereligne,". MAXSHOW;

//exécuter la requête
$result1 = mysqli_query($connection, $sql1);

if(empty($motcle) ) {$warning= "<span style='color:red; font-family:arial; font-size:12px;'>Veuillez fournir un critère de recherche ici en haut, svp!</span>";}

?>
<html>
<head>
<title>
Formulaire de vérification de l'existence d'un mémoire
</title>
<!--<link href="css/ulbu.css" rel="stylesheet" type="text/css"/>-->
<style>html,body{
background-color:#dea;
height:100%;
margin:0;
overflow:auto;
}

body{
text-align:center;
color:#abc;
font-family: "Trebuchet MS",Arial, Tahoma, sans-serif;
color:#5b1c46;
margin:0 auto;
}

a{
text-decoration: none;

}

a:link{
color:RGB(10%,80%,20%);
}

a:visited{
color:RGB(40%,10%,60%);
}

a:active, a:focus{
color:#adf;
}

a:hover{
color:#339999;
}

div#conteneur{
width:1024px;
margin:0px auto;
padding:0 0;
background:#fff;
text-align:left;
padding: 10px 20px;
padding-bottom:0;
height:auto;

min-height:100%;
border:2px solid #ab4;
position:relative;
}

html > body div#conteneur{
height:auto;
min-height:80em;
}

div#header{
height:10%;
overflow:hidden;
background:;
width:1024px;
border-bottom:1px dashed gray;
}

div#wrapper{
width:100%;
position:relative;
}

div#menu{
width:200px;
float:left;
}

div#menu ul{
margin: 0 0;
padding:0;
text-align:right;
position:relative;
list-style-type:none;
}

div#menu ul li{
padding-right:15px;
font-variant:small-caps;
border-bottom:1px dotted RGB(90%,40%,20%);
padding:10px;
}

div#contenu{
float:right;
position:relative;
width:800px;
border-left:1px dashed #339999;
padding-left:20px;

}

div#contenu p{
margin-top:10px;
}

div#contenu form label{
width:100%;
display:block;

}

div#contenu form fieldset label, span{
display:block;
}

div#contenu form fieldset label span{
float:left;
width:45%;
text-align:right;
color:RGB(90%,40%,20%);
display:block;

}

div#contenu fieldset{
height:200px;
}
div#contenu form fieldset label span, label input{
display:inline;
}
div#contenu form fieldset label input{
margin-left:20px;
float:right;
}


div#contenu form fieldset label span, label select{
display:inline;
}

div#contenu form fieldset label select{
float:right;
}

div#contenu form fieldset label select option{
color:RGB(90%,40%,20%);
}

fieldset label#last{
position:relative;
top:50px;
}

fieldset label#second{
position:relative;
top:10px;

}

fieldset label#last input{
float:right;
right:400px;
position:absolute;
margin-top:40px;
}
html > body div#contenu{
height:auto;
min-height:75em;
margin-bottom:0;
}

div#menu, div#contenu{
display:inline;
}</style>
</head>
<body>
<div id="conteneur">
<div id="header">

</div> <!--fin div header -->
<div id="wrapper">
<div id="menu">
<!--<?php include("includes/menu.inc.php");?>-->
</div> <!--fin div menu -->
<div id="contenu">
<p>Bienvenu au module de recherche d'un mémoire. Cette page est une interface vous permettant de vérifier si le mémoire que vous voulez entreprendre d'ici peu n'a pas encore été traité par quelqu'un d'autre dans le passé!</p>

<form method="POST">
<fieldset>

<label for="motcle">
<span>Veuillez fournir votre mot clé de recherche:</span><input type="text" name="motcle" size="60" value="<?php if(isset($motcle)) echo $motcle;?>"></label>
<label id ="second" for="faculte"><span>Faculté:</span><select name="faculte"><option value="Informatique de Gestion">Informatique de gestion</option><option value="Communication">Communication</option><option value="Gestion et Administration">Gestion et Administration</option><option value="Droit">Droit</option> </select></label>

<label id="last"><input type="submit" name="rechercher" value="Rechercher" id="submit"></label></fieldset>
</form>
<?php
if(isset($warning) ) {echo $warning;}
if(isset($totals)){
echo "<p><b>$totals mémoires correspondent à vos critères de recherche. </b></p>" ;
echo "<p>----------------------------------------------------------------------------------------------------------------------</p>";
}


if($result1){

while($row = mysqli_fetch_assoc($result1)){
$auteurs = $row["auteurs"];
$sujet = $row["sujet"];
$directeur = $row["directeur"];
$faculte = $row["faculte"];
echo "<p><b>Sujet:</b> $sujet<br> <b>Auteurs:</b> $auteurs<br> <b>Directeur:</b> $directeur<br> <b>faculté:</b> $faculte</p>";
echo "*******************************************************************";
}


echo "<br>Enregistrement n° ". ($numero_premiereligne+1);
if($numero_premiereligne + 1 < $totals)
{
echo " à ";
if($numero_premiereligne + MAXSHOW < $totals)
{
echo $numero_premiereligne + MAXSHOW;
}
else {
echo " $totals ";
}
}
echo " sur $totals enregistrements.&nbsp;&nbsp;";

// Mettre les liens de navigation
if($page_courante == 0){

echo '<a href="'.$_SERVER['PHP_SELF'].'?page_courante='.($page_courante + 1).'">Suivant&nbsp;&gt;</a>&nbsp;&nbsp;';

echo '<a href="'.$_SERVER['PHP_SELF'].'?page_courante='.(ceil($totals/MAXSHOW)-1).'">Fin&nbsp;&raquo;</a>&nbsp;&nbsp;';

}

if($page_courante > 0){
echo "<a href='".$_SERVER['PHP_SELF']."?page_courante=".'0'."'>&laquo;Début&nbsp;</a>&nbsp;&nbsp;";
echo "<a href='".$_SERVER['PHP_SELF']."?page_courante=".($page_courante-1)."'>&laquo;Précédent</a>&nbsp;&nbsp;";

if($numero_premiereligne + MAXSHOW < $totals)
{

echo "<a href='".$_SERVER['PHP_SELF']."?page_courante=".($page_courante + 1)."'>Suivant&nbsp;&gt;</a>&nbsp;&nbsp;";

echo "<a href='".$_SERVER['PHP_SELF']."?page_courante=".(ceil($totals/MAXSHOW)-1)."'>Fin&nbsp;&raquo;</a>&nbsp;&nbsp;";

}
}
}

else {echo "<span>There is no resultset!</span>";}


?>
</div> <!--fin div contenu -->
</div> <!--fin div wrapper -->
</div> <!--fin div conteneur -->

</body>
</html>

dbarasuk

10:53 am on Dec 15, 2009 (gmt 0)

10+ Year Member



Can someone please look at this code to help me?

ALKateb

12:08 pm on Dec 15, 2009 (gmt 0)

10+ Year Member



can you be more precise about the problem please? or at least provide a sample of your database please

rocknbil

6:52 pm on Dec 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code dump is too huge, you need to quantify the problem in a smaller script - which may lead you to a solution.
The problem, though, is probably here. What do you get when you add the bolded echo?

$page_courante = isset($_GET["page_courante"])?$_GET["page_courante"]:0;
$numero_premiereligne = $page_courante*MAXSHOW;
$sql1 = "SELECT * FROM memoires WHERE sujet RLIKE '$motcle' AND faculte ='$faculte' LIMIT $numero_premiereligne,". MAXSHOW;

echo '<!-- count ' . $page_courante . ' select ' . $sql1 . '-->';

And what do you get when you copy and paste the resulting $sql1 into mysql command line or phpMyAdmin?

dbarasuk

7:27 am on Dec 16, 2009 (gmt 0)

10+ Year Member



The code was made huge by the CSS code inside just for you to see how the page looks like if you copy it on your local machine.

There is no problem for the sql code because if run it on a database with enough data it returns a couple of records that contain the keyword it type in the search textbox.

The problem occurs when i click the "Suivant" link to see the rest of the resultset because it sends back an empty page which means there is a problem.

However, if i type again the same keyword in the textbox and hit again the search button, i am surprised to see that the page returns the result set i was supposed to get if the page had not returned an empty page in the previous stage. To remediate to this problem, i put a php code (see in the code) in the search textfield to always echo the value of the keyword that was passed to the script once it has been provided, but this seems not to yield the expected result and it is on this very point i would like to get your help.

Tommybs

7:37 am on Dec 16, 2009 (gmt 0)

10+ Year Member



It's because you are using $_POST variables. The value is only sent once to the server, so the second you navigate to a new page, even if it is just via querystring params, that value is lost. You could try using ajax or something similar to just update the paging area (though you will need to pass in the keyword parameter), or as the title suggests, swtich the $_POST of the search keyword to a $_GET, validate it, and hold it in the querystring for access on each page.

dbarasuk

8:19 am on Dec 16, 2009 (gmt 0)

10+ Year Member



I changed the submit method from POST to GET. The first time the page works with result but again does not provide any result while clicking the navigation links.

Tommybs

8:26 am on Dec 16, 2009 (gmt 0)

10+ Year Member



Are you forming your page links with the searchword in the querystring
e.g page.php?p=2&word=search_word

?

ALKateb

11:50 am on Dec 16, 2009 (gmt 0)

10+ Year Member



dbarasuk

the problem is that you are not passing the search parameters with the next button as the guys said already

you should make those changes for your code to work:

change these:
$motcle = isset($_POST['motcle'])? mysqli_real_escape_string($connection, strip_tags(trim($_POST["motcle"]))):"";
$faculte = isset($_POST['faculte'])? mysqli_real_escape_string($connection, strip_tags(trim($_POST["faculte"]))):"";

to:

$motcle = isset($_REQUEST['motcle'])? mysqli_real_escape_string($connection, strip_tags(trim($_REQUEST["motcle"]))):"";
$faculte = isset($_REQUEST['faculte'])? mysqli_real_escape_string($connection, strip_tags(trim($_REQUEST["faculte"]))):"";

u see here all i did is changed _POST to _REQUEST so it can work for both _POST and _GET

and also change this line:
echo '<a href="'.$_SERVER['PHP_SELF'].'?page_courante='.($page_courante + 1).'">Suivant&nbsp;&gt;</a>&nbsp;&nbsp;';

to this:

echo '<a href="'.$_SERVER['PHP_SELF'].'?page_courante='.($page_courante + 1). '&motcle=' . $_REQUEST['motcle'] . '&faculte=' . $_REQUEST['faculte'] . '">Suivant&nbsp;&gt;</a>&nbsp;&nbsp;';

i tested it and it works so you apply the same for the other links (previous,final, beginning .. etc)

the whole problem was that you are not passing the search parameters to the next page all you were passing is the (page_courante) so how can the query be built without those variables?

so the way to pass them is to put them in the link (next) but you were catching only $_POST variables so this will be solved when catching both GET and POST variables by using $_REQUEST instead
i hope i could help

dbarasuk

7:29 pm on Dec 17, 2009 (gmt 0)

10+ Year Member



AlKateb,
You are completely right. I was not passing the other variables used in the query and that was the source of all the headaches.

Before i read your suggestion i changed $_POST to $_GET and passed in the other search parameters and now everything is all right. Thank you to let me know _REQUEST replaces both GET and POST

CyBerAliEn

8:27 pm on Dec 17, 2009 (gmt 0)

10+ Year Member



Small point... I would recommend always using _REQUEST. You then make the code independent of whether it was POST/etc.

But congrats on getting it to work! :)

Tommybs

7:30 am on Dec 18, 2009 (gmt 0)

10+ Year Member



I would only use REQUEST if you absolutely trust what's going on. Would you use REQUEST on an Ecomm site for the shopping basket? The most likely answer is no...otherwise I could send people to your site with a querystring adding a number of products to their basket. I don't want to stray off topic, but in my opinion, you should only accept data in a format you expect it sent in and request just makes it that little bit easier for the bad guy