Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 30th Oct 2013, 10:22 PM   #1
Active Warrior
 
Join Date: 2010
Location: Malaysia
Posts: 36
Thanks: 14
Thanked 0 Times in 0 Posts
Default
php script to echo html form
Share on: 
fb share twitter share gplus share more share

please i need help on this i am trying to echo HTML form in php script but i am having a logical error, the html checkbox control displays first before the variable $Question displays i dont understand why.

this is my code

<form action = "calculateResult.php" method = "post">
<?php

$con=mysqli_connect("localhost","root","","online_ exam");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM question");
//$AnsResult = mysqli_query($con,"SELECT * FROM answers");

$i = 1;
while($row = mysqli_fetch_array($result))
{
$i = $i + 1;
$question = $row['Quest'];
print("$question");
echo '<br/>';
$Q1 = $row['answer1'];
$Q2 = $row['answer2'];
$Q3 = $row['answer3'];
$Q4 = $row['answer4'];

echo '<input type = "checkbox" value = "hi" name = "answers"/>'.$Q1;
echo '<br/>';
echo '<input type = "checkbox">'.$Q2;
echo '<br/>';
echo '<input type = "checkbox">'.$Q3;
echo '<br/>';
echo '<input type = "checkbox">'.$Q4;
echo '<br/>';

}
mysqli_close($con);
?>
<input type = "submit" value = "SUBMIT"/>
</form>





this statement print("$question"); is suppose to execute before this other statements but the echo '<input type = "checkbox" value = "hi" name = "answers"/>'.$Q1; execute before the print("$question"); statement that is what i mean
echo '<input type = "checkbox" value = "hi" name = "answers"/>'.$Q1;
echo '<br/>';
echo '<input type = "checkbox">'.$Q2;
echo '<br/>';
echo '<input type = "checkbox">'.$Q3;
echo '<br/>';
echo '<input type = "checkbox">'.$Q4;
echo '<br/>';

Last edited on 31st Oct 2013 at 11:11 AM. Reason: for clearity and more inderstanding
lloydblinks is offline  
Unread 31st Oct 2013, 02:09 AM   #2
Web Developer
War Room Member
 
Andrew H's Avatar
 
Join Date: 2012
Location: Vancouver, BC
Posts: 593
Thanks: 149
Thanked 162 Times in 133 Posts
Default
Re: php script to echo html form
Share on: 
fb share twitter share gplus share more share

Because you are echo'ing out the checbbox input before the question


echo $Q1.'<input type = "checkbox">';


The questions you are asking are insanely simple (looking at your other question as well). Your script is broken in many other ways as well, learn some PHP (it's simple, i promise) before asking too many more question.

"You shouldn't come here and set yourself up as the resident wizard of oz."
Andrew H is offline  
Unread 31st Oct 2013, 11:10 AM   #3
Active Warrior
 
Join Date: 2010
Location: Malaysia
Posts: 36
Thanks: 14
Thanked 0 Times in 0 Posts
Default
Re: php script to echo html form
Share on: 
fb share twitter share gplus share more share

this statement print("$question"); is suppose to execute before this other statements but the echo '<input type = "checkbox" value = "hi" name = "answers"/>'.$Q1; execute before the print("$question"); statement that is what i mean
echo '<input type = "checkbox" value = "hi" name = "answers"/>'.$Q1;
echo '<br/>';
echo '<input type = "checkbox">'.$Q2;
echo '<br/>';
echo '<input type = "checkbox">'.$Q3;
echo '<br/>';
echo '<input type = "checkbox">'.$Q4;
echo '<br/>';
lloydblinks is offline  
Unread 31st Oct 2013, 05:13 PM   #4
Web Developer
War Room Member
 
Andrew H's Avatar
 
Join Date: 2012
Location: Vancouver, BC
Posts: 593
Thanks: 149
Thanked 162 Times in 133 Posts
Default
Re: php script to echo html form
Share on: 
fb share twitter share gplus share more share

Not really sure what you are saying the problem is. Here is a good resource: PHP: Hypertext Preprocessor

"You shouldn't come here and set yourself up as the resident wizard of oz."
Andrew H is offline  
Unread 1st Nov 2013, 08:20 AM   #5
VIP Warrior
War Room Member
 
Join Date: 2006
Location: , , USA.
Posts: 1,795
Thanks: 70
Thanked 287 Times in 259 Posts
Default
Re: php script to echo html form
Share on: 
fb share twitter share gplus share more share

Are you sure the data in the table "question" is correct?
KirkMcD is offline  
Unread 1st Nov 2013, 02:39 PM   #6
Bananas are Yellow!
Registered Member
War Room Member
 
David Beroff's Avatar
 
Join Date: 2013
Location: Wyoming, PA, USA
Posts: 1,046
Thanks: 2,549
Thanked 1,600 Times in 928 Posts
Blog Entries: 4
Default
Re: php script to echo html form
Share on: 
fb share twitter share gplus share more share

Why are you mixing print and echo? There doesn't seem to be any reason for this. See this comment in the documentation.

Put MY voice on YOUR video: AwesomeAmericanAudio.com
David Beroff is offline  
Unread 1st Nov 2013, 09:38 PM   #7
HyperActive Warrior
War Room Member
 
drem's Avatar
 
Join Date: 2011
Posts: 369
Thanks: 113
Thanked 86 Times in 79 Posts
Default
Re: php script to echo html form
Share on: 
fb share twitter share gplus share more share

I believe what you are saying is that for some reason print("$question"); prints after all of the rest. This would occur when the $row['Quest'] is null. So, during the first loop there is nothing inside of it to print. After, it goes into the loop again and prints out whatever is in the array.

Otherwise, supply some more data so we can figure out what is going on.

drem is offline  
Unread 2nd Nov 2013, 07:18 AM   #8
Active Warrior
War Room Member
 
Join Date: 2012
Posts: 71
Thanks: 2
Thanked 13 Times in 13 Posts
Default
Re: php script to echo html form
Share on: 
fb share twitter share gplus share more share

Created example for you here:
[PHP] <?php $questions = array( 'question1' => array( 'answer1' => - Pastebin.com

Create array of returned MySql data and it should work.

Lani is offline  
Closed Thread


Bookmarks

Tags
echo, form, html, php, script


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 11:45 PM.