Jump to content

HTML form controls not reading php variables


Irish_Dave

Recommended Posts

Hi everyone.

I am David and I am new to web design and php. I am following a Lynda.com tutorial on php and sql.

I have reached a certain point in the tutorial where I am reading values back from my database in to a php variable. The variable is holding an array data type and I am accessing the element within the array.

 

I want a HTML select control and 2 radios buttons to reflect the values in the database.

Therefore the Select control should read a numeric value of type int and the radio buttons are reflecting either a 1 or a 0.

 

Firstly the code for the Select control uses a for loop to count the array elements and load the count into the Select control. The example uses php mixed with html. The for loop will count the number of element and add a number to the select control. The an If statment is used to make a decision if the count variable equals the value in the array element $sel_subject['position'] then select that value. This does not happen, and I can not seem to get it to work.

 

                                          <p>Position:

<select name="position">

<!--use a php block so as to use some php variables -->

<!--Use these variables with the get_all_sublects function. -->

<!-- This function will return all the dataset -->

<?php

$sel_subject = get_all_subjects();

//Then use the mysql fuction to retuen the numberic value of all the rows.

$subject_count = mysql_num_rows($sel_subject);

//$subject_count + 1 becauce we are adding a subject

for($count=1; $count <= $subject_count+1; $count++)

{

echo "<option value=\"{$count}\"";

 

if ($sel_subject['position'] == $count)

{

echo " selected";

}

 

echo ">{$count}</option>";

}

?>

</select>

</p>

 

Secondly the radio buttons do something similar in that i want them to reflect the value in the array element. If a 1 then True and so be checked or 0 and so false and so be unchecked.

 

                                            <p>Visible:

 

<input type= "radio" name= "visible" value= "0"

<?php if ($sel_subject['visible'] == 0)

{

echo 'checked=" checked"';

}

?>

>No</input>

 

<input type= "radio" name= "visible" value= "1"

<?php if ($sel_subject['visible'] == 1)

{

echo 'checked=" checked"';

}

?>

>Yes</input>

</p>

 

Again this doesn't work either. So in summary, I cant get the php code to affect the html form controls and relay the stored data to the user.

I have been racking my brain for days and getting no where. Please can anyone help me with this.

 

I would be very grateful.

All the best Irish_Dave

 

Link to comment
Share on other sites

are you sure your get_all_subjects(); is connecting to the database and returning any data, as you havent provided this bit of code(minus any usernames and passwords) we cant see what is going on, we cant see where your variables are meant to be coming from

Link to comment
Share on other sites

Hi dragon_sa, and thank you for taking the time to read my post.

 

I am quite sure the php function get_all_subjects() returns an array to the php variable $sel_subject

because the php code outputs the menu name selected to the input text box.

 

                                            <p>Subject Name:

<input type="text" name="menu_name" value="<?php echo

                                                      $sel_subject['menu_name']; ?>" id="menu_name"/>

      </p>

 

I then replaced the $sel_subject['menu_name'] with $sel_subject['position'] to see if the position was outputted to the text box and it was.

I have attached the functions.php along with the connection, constants, and edit_subject.php to see if that would make what I am trying to solve any clearer.

 

So in summary, I can access the array elements and output them to a text field but not to a select control or radio button.

                                   

17977_.php

17978_.php

17979_.php

17980_.php

Link to comment
Share on other sites

Hey there Irish_Dave,

 

I am also fairly new to PHP. I have gone over your code and the html part of it all looks like its in order.  You for loop that creates the selector's options works when I hard coded in a number for $subject_count and for $sel_subject['position']. I set $sel_subject['position'] to 3 and $subject_count to 6 and it came out fine with the third option being selected. So that tells me that the array containing the result for $sel_subject['position'] doesnt exist.

 

Try these debugging strategies:

make sure the information being returned is correct in your database by using print_r():

                        $sel_subject = get_all_subjects();

echo "<pre>";

print_r($sel_subject);

echo "</pre>";

                        //Then use the mysql fuction to retuen the numberic value of all the rows.

                        $subject_count = mysql_num_rows($sel_subject); /* The results contained in $sel_subject HAS to be gotten using MySQL SELECT or SHOW commands only */

echo $subject_count;

 

                        //$subject_count + 1 becauce we are adding a subject

                        for($count=1; $count <= $subject_count+1; $count++)

                        {

                          **your code for creating the html options**

                        }

 

Making sure the information being returned is correct is a good start to debugging why your script isnt working. Say for instance $subject_count was returning a 0, then your for loop would run and but would only create 1 option and your $sel_subject['position'] would not be echo'd. Also i would comment out the for loop for the moment since its not working at the moment anyways.

 

It depends on what the print_r($sel_subject) and echo $subject_count; returns to decide where the next problem might or could be.

 

I am sorry if this post came out very sloppy and hard to read. This is my very first post in this forum. This is just where i would start with your code as nothing jumps out at me as wrong.

 

I hope this helped Irish_Dave but understand it might not have lmao

 

thanks

 

algidDes702

Link to comment
Share on other sites

Hi algidDes702.

 

Thanks for taking the time to look in to my problem. Thanks for the debugging tips. I am very new like I said to php. I have applied what you said, and echoed what was returned from the $sel_subject = get_all_subjects()

which basically runs a query on the sql database. I got a"Resource id #14" returned not an array like i was expecting. Not sure what this means.

I also echoed $subject_count = mysql_num_rows($sel_subject); and I got 4 returned which matched what i would expect if I had an array returned from the get_all_subjects() function.

 

I have attached the functions and connection code that are needed for the sql query.

 

Not sure what to do now? I suppose I need to verify absolutely the contents of the $sel_subject, some how see whats in the variable.

 

17985_.php

17986_.php

17987_.php

17988_.php

Link to comment
Share on other sites

Alter your get_all_subjects with the following.

 

function get_all_subjects() 
{

	global $connection;
	//put the query into a variable.
	$query = "SELECT * FROM subjects ORDER By position ASC";
	$subject_set = mysql_query($query, $connection);
	confirm_query($subject_set);
                $subjects = array();
                while($subjects[] = mysql_fetch_assoc($subject_set));
	return $subjects;

}

 

That'll return an array of your "subjects".

Link to comment
Share on other sites

Hi CPD.

Thanks for the help, i added the code and it returned an array with all the sql data set in it. So this must prove the  funtion is returning a data set, or also known as a resource as I have read in other sites. So it just begs the question why will my html select and radio controls not reflect the values in the associative array?? Still confused.

 

Thanks to everyone so far for the input, it all helps just not got it working yet.

Link to comment
Share on other sites

Hi everyone.

To try and  make it a bit more clear what I am seeing, I have attached a screen dump of what i get in my browser and of my SQL database.

 

So just to summaries my problem again: I am running an sql query on my database returning a data set.

I am using this data set to create my navigation on the left of my browser.

When I select a menu subject I get the edit_page.php showing the Subject/Menu selected in a text field but I want the Position value reflected in the html select control and the visibility No/Yes (1/0 numeric values) reflected in the radio buttons. I can  not get the html select or radio buttons reading the php associative array ($sel_subject['position'] or $sel_subject['visible']).

How can I read $sel_subject['menu_name'] in to the input text field but not the other two elements of the associative array.

Very confusing.

post-132971-13482403394645_thumb.jpg

post-132971-13482403394903_thumb.jpg

17993_.php

Link to comment
Share on other sites

Hi everyone, I have managed to resolve the issue of making my html form controls reflect what i am reading back from my SQL database.

 

It was rather straight forward in the end but it was more by accident rather than anything else because I am not understanding quite why it works.

I made the following variable access the associative array as shown below:

 

                    <?php $catch_position = $sel_subject['position']; ?>

<?php $catch_visible = $sel_subject['visible']; ?>

 

There are two issues confusing me :

 

1) is the fact that I put the line of code before the function call $sel_subject = get_all_subjects() surely they should be placed after the function call but when I do that it doesn't work.

 

2) Why could I not access the associative array directly.

 

A real scratch your head moment. Glad it works but depressing the fact the logic doesn't make sense.

If anyone has come across this before and knows why then I would love you to share it with me.

 

Thanks again to everyone who offered help/knowledge in this problem

17999_.php

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.