PHP - English

PHP Setting a Question for the Survey

 

 

This lesson is part of an ongoing Survey/Poll tutorial. The first part is
here: Build your own Survey/Poll, along with all
the files you need.

 

In the previous part of this lesson, you had a look
at the structure for the Survey database.The first PHP page we’ll take a look
at is the setSurvey.php file, which is in the scripts/survey
folder that you downloaded. The is where we set a question for the survey.
As a reminder, the page looks like this in a browser:

Using your favourite text editor, open up the file called setQuestion.php
again, and take a look at the code:

(opens in a new browser tab as a text file)

At the very top of the page, we have our require line, which gets the
constants for the database and server connection. Notice that the file location
reference has two sets of dot/dot/slash at the start. This is because we have
placed our configure file outside of the web root folder. You should
change the file reference to match where you have saved your own configure file.

After getting a reference to the configure file, we set up some variables.
Most of the variables are set to blank strings. Except these two:

$startSelect = “<SELECT NAME=drop1>”;
$endSelect = “</SELECT>”;

We have to construct our own HTML dropdown box with PHP. If you were doing
it purely in HTML then a dropdown box looks like this:

<SELECT>

<OPTION Value=”VALUE_HERE”>ItemOne Here</OPTION>
<OPTION Value=” VALUE_HERE “>Item Two Here </OPTION>

</SELECT>

The above HTML code would give you a dropdown list with two items in it. So
we need to add the two SELECT parts, the beginning and the end ones, using PHP
code. We’ll be filling in the OPTION parts later.

When the page first loads, we’ll fill it with options from the database table.
However, we need to detect if the button on the form was clicked. The next part
of our PHP code detects that:

if (isset($_GET[‘Submit1’])) {

$getDropdownID = $_GET[‘drop1’];
header (“Location: survey.php?h1=” . $getDropdownID);

}

We’ll come back to this code later. But it checks if a HTML Form object called
Submit1 has been set. This form object is the button. We want to redirect to
the survey page, where the survey the user selected will be loaded.

The first thing the rest of the code does is to connect to the server and database.
We then have this line:

$stmt = $db_found->prepare(“SELECT ID, Question
FROM tblsurvey”);

if ($stmt) {

//REST OF CODE HERE

}

The first line is a prepared statement. In between round brackets, we have
this:

SELECT ID, Question FROM tblsurvey

In previous tutorials, we selected all the records from a database table. We
used the asterisk symbol (*):

SELECT * FROM tblsurvey

However, you don’t need to select every field from your table. You can select
only the fields (table columns) that you need. We only need the ID number and
the question, so we’re just selecting these two fields. The question will be
added to the dropdown box. The ID number is placed in a hidden HTML tag. When
we redirect to the survey page, survey.php will grab this ID and use it to pull
the correct records from the table.

The if ($stmt) statement is testing that we actually got a result back from
that SQL. If we did, the variable have a value of true. If the SQL failed then
$stmt will be false. We’ve added an else part to print something out, if the
SQL failed:

if ($stmt) {

//REST OF CODE HERE

}
else {

print “Error – DB ERROR”;

}

If the SQL is successful then we execute it, and get the results:

$stmt->execute();
$res = $stmt->get_result();

Next, we test if the number of rows is greater than zero. If it’s not we print
a message:

if ($res->num_rows > 0) {

//REST OF CODE HERE

}

else {

print “Error – No rows”;

}

If rows were found, we can loop round and construct our dropdown HTML code:

while ( $row = $res->fetch_assoc() ) {

$qID = $row[‘ID’];
$question = $row[‘Question’];
$dropdown = $dropdown . “<OPTION VALUE='” . $qID . “‘>”
. $question . “</OPTION>” . “<BR>”;

}

$wholeHTML = $startSelect . $dropdown . $endSelect;

The loop goes round and round while there are rows in the table. The first
thing we do is to grab the ID and Question values from the table:

$qID = $row[‘ID’];
$question = $row[‘Question’];

The row ID goes into the $qID variable we set up at the top of the code; the
question from the row goes into a variable of its own, $question. Once we have
these values, we can use them to construct the HTML:

$dropdown = $dropdown . “<OPTION VALUE='”
. $qID . “‘>” . $question . “</OPTION>” . “<BR>”;

We only need the OPTION part of the SELECT tag, here. We’re adding the $qID
as the VALUE parameter. The question itself goes into between pointy brackets.
Once the loop has done its work, when can piece together the whole of the HTML
for a dropdown list:

$wholeHTML = $startSelect . $dropdown . $endSelect;

We can use the $wholeHTML variable in the FORM part of the HTML. Here’s the
HTML for the FORM:

<FORM NAME =”form1″ METHOD =”GET”
ACTION =”setSurvey.php”>

<?PHP print $wholeHTML; ?>

<P><INPUT TYPE = “Submit” Name = “Submit1”
VALUE = “Set a Question”></P>

<?PHP print $hidTag; ?>

</FORM>

The ACTION for this form is to send it to itself for processing. It does this
when the button with the TYPE called “Submit” is clicked. The NAME
of this Submit button is “Submit1”.

Notice the print statement, though:

<?PHP print $wholeHTML; ?>

The variable $wholeHTML contains our dropdown list.

 

Button Clicks

We detect if the button on the form was clicked with this PHP code, which is
at the top:

if (isset($_GET[‘Submit1’])) {

$getDropdownID = $_GET[‘drop1’];

header (“Location: survey.php?h1=” . $getDropdownID);

}

This detects if the button with the NAME Submit1 was clicked. If it was, we
have this as the first line:

$getDropdownID = $_GET[‘drop1’];

We’re trying to get the NAME of the HTML SELECT dropdown box. If you remember,
we had this for the SELECT:

$startSelect = “<SELECT NAME=drop1>”;

This is where the name “drop1” comes from. Once we have the NAME
of the SELECT dropdown list, we can grab whatever VALUE was selected from the
list and store it in the variable called $getDropdownID. The VALUES are
coming from this part of the code:

“<OPTION VALUE='” . $qID . “‘>”
. $question . “</OPTION>”

Each OPTION VALUE will be different, and comes from the ID field in the database
table.

The next line is a redirect:

header (“Location: survey.php?h1=” . $getDropdownID);

We want to redirect to the survey.PHP page. But we need this page to have an
ID. That way, the code for the survey page can grab the ID value and use it
to look up the correct survey. Because we’re using GET as our FORMs METHOD,
we can do this with the web address:

survey.php?h1=”

The question mark means a value follows. The name of the value is something
that we have called h1. But you can call it almost anything you want. After
an equal sign, you type the value that you want to send to the page. In our
case, this value is contained in the variable $getDropdownID.

And that’s the code for the setSurvey page. It is a little complex, but study
it closely and hopefully it will make sense. But all we’re doing really is grabbing
an ID from a database table and sending it to another page. That page is survey.php.
We’ll take a look at the PHP code for that now.

|

Kaynak : https://www.homeandlearn.co.uk/php/php15p3.html ‘sitesinden alıntı

Yorum Yap