SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to make multiple checkbox values for a form sticky

    Hello again everyone .

    i am new to PHP but steadily improving my skills. i am building my first application and hope to complete it within a week.

    i have two questions to ask. But before i start , i first i wish to thank everyone at sitepoint- particularly Anthony Sterling for their kind support and help.


    QUESTION ONE



    HOW TO MAKE MULTIPLE CHECKBOX VALUES IN A FORM STICKY

    i am building a sticky multi checkbox value for a form.

    i have adapted a function that was previously given to me at sitepoint but i can't seem to get it to work.

    PHP Code:


    $jobs 
    ''




    if(isset(
    $_POST['submit']))
      {

               require_once (
    'db_fns.php'); // Connect to the db.



    $job_sought $_POST['job_sought'];
    foreach (
    $job_sought as $job
        {
    $source .= $jobs.", ";}
        
    $jobs_sought substr($source0, -2);




            
            
    $q3 "INSERT INTO typeworksought ( user_id, job_sought  ) 
            VALUES ( '
    $id','$jobs_sought' )";        

        
            
    $R3 mysqli_query ($dbc$q3); // Run the query.      


    }




     <
    form action" " 
            
    method="post" name="form1" >

        
         
    $job 'job';  
            
            
        function 
    selected($job){
              
              if(empty(
    $_POST['job'])){
                return;
              }
              
              if(!
    is_array($_POST['job'])){
                return;
              }
              
              if(!
    in_array($job$_POST['job'])){
                return;
              }
              
              return 
    'selected="selected"';
            }




       
       if(isset(
    $_POST['job_sought'])){  
              
         echo
              
    ' <input type="checkbox"  name="job_sought[]" value= "'.$job.'"  
              '
    .selected($job);'    /> job' 
               }
              else
          
                 { 
              
              echo 
    '<input type="checkbox" name="job_sought[]" value="job"   /> job'

                  
              } 
              
             
    </
    form
    Two questions regarding my script.
    1; any ideas why it is not sticky.

    2; is there a better formula for a checkbox sticky form. my concern with my function is that it will be cumbersome if i have many checkboxes in a category. it will means that i will have to draft the same function for each of the checkboxes.

    i want to insert all the full values of the submitted checkboxes into the mysql database so that those values can be indexed and searched upon later on



    QUESTION TWO;

    How do i maintain a session ID in non PHP pages

    i have build a website comprising php and html pages. for all the PHP pages i have placed a

    PHP Code:
    session_start(); 
    at the top of the page.

    i did this to maintain a session ID for the user. however, the site also contains non php pages, i.e html. the problem is that if a user goes to the Html page and then want to come back to the php page later on they will lose their session Id.

    is there a way that i can maintain their session ID even when they are in non php pages? is it possible? or most i convert all the pages to PHP.

    thank you everyone for your kind assistance.

    sweet thanks

    Andreea

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    1: 'sticky'. This word means nothing to me. Please define it? You use it like it's got context.

    2: Make all the pages .php and stick <?php session_start(); ?> at the top of them. (You can also set apache to parse .html through the PHP engine, but that's a non-ideal solution IMO.) PHP cant maintain a session if it isn't run.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Starlion

    i am sorry. i thought that sticky forms was a standard expression.

    what i mean by sticky is that the form retains the information that the user has clicked.

    for example. if i have 5 options in a check box value and I click one of those options then those options will be retained and showed by the form if the form is returned to the user because he did not click all the questions.

    the start of the thread is in the link below. you can see how and where i got the original function from.

    http://www.sitepoint.com/forums/php-...on-760210.html


    i hope this helps. once again deepest apologies for making up a word

    kind regards

    Andreea

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Ahhh okay. Returning the same data. Gotcha.

    Well, lets take a look at your code. I'm going to condense the blank lines out of it for the sake of my eyes.

    PHP Code:
       if(isset($_POST['job_sought'])){  
         echo 
    ' <input type="checkbox"  name="job_sought[]" value= "'.$job.'"          '.selected($job);'    /> job' 
               } 
    (we'll ignore the ELSE for now, though that's got issues)
    EDIT: Er. Blind me. Okay...

    PHP Code:
              if(empty($_POST['job'])){
                return;
              } 
    I see no field for job in your form. I see one for job_sought, but not job.... so $_POST['job'] is not set, so it is returning.

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi.

    thank you for your speedy responce.

    i am a little bit confused at your answer though. ( sorry but i am still qyou

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry but i am still quite new to PHP.

    how are you able to return ;

    if(empty($_POST['job'])){
    return;
    }

    i wold have thought that the jobs was simply an element in an array of

    <input type="checkbox" name="job_sought[]" value= "'.$job.'" '.selected($job);' /> job'

    i therefore dont understand how i am therefore able to return a single element of that post array .

    kind regards

    Andreea

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Okay. Crash course on form handling.


    <form action= " " method="post" name="form1" >
    <input type="checkbox" name="job_sought[]" value= "'.$job.'" '.selected($job);' /> job'

    Method='post' says that the variables will be put into the $_POST variable on submission.
    the NAME of each input defines the index of $_POST that will be used.

    So in this case, your form will populate $_POST['job_sought'] (which will be an array, because of the additional []'s). It's not populating $_POST['job'], which is what the function is looking for.
    Change all of the $_POST['job'] to $_POST['job_sought'] in that function declaration, and it should work.

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello again

    thank you taking the time out to do a quick crash course. i understand the basic concept and have indeed scripted a large form. the only issue that i am stuck with is to return the values for these checkboxes.

    i have followed your instructions but it is still not returning the values to the form. i have enclosed below the full script. i would be very grateful if you could tell me where i have gone wrong with it ( as far as i can make out, it looks correct and should return the values to the form).



    PHP Code:


    $jobs 
    ''
    if(isset(
    $_POST['submit']))


      {           require_once (
    'db_fns.php'); // Connect to the db.
     
    $job_sought $_POST['job_sought'];

    foreach (
    $job_sought as $job

        {
    $source .= $jobs.", ";}

        
    $jobs_sought substr($source0, -2);

            
    $q3 "INSERT INTO typeworksought ( user_id, job_sought  ) 

            VALUES ( '
    $id','$jobs_sought' )";        

            
    $R3 mysqli_query ($dbc$q3); // Run the query.      

    }

     <
    form action" "     method="post" name="form1" >

         
    $job 'job';  
            
            
        function 
    selected($job){
              
              if(empty(
    $_POST['job_sought'])){
                return;
              }
              
              if(!
    is_array($_POST['job_sought'])){
                return;
              }
              
              if(!
    in_array($job$_POST['job_sought'])){
                return;
              }
              
              return 
    'selected="selected"';
            }

            
       
       

       if(isset(
    $_POST['job_sought'])){  

              

         echo

              
    ' <input type="checkbox"  name="job_sought[]" value= "'.$job.'"  

              '
    .selected($job);'    /> job' 

               }

              else

          

                 { 

              

              echo 
    '<input type="checkbox" name="job_sought[]" value="job"   /> job'


              } 

  9. #9
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Try doing this:
    PHP Code:
        function selected($job){
              
              if(empty(
    $_POST['job_sought'])){
                return 
    '3';
              }
              
              if(!
    is_array($_POST['job_sought'])){
                return 
    '2';
              }
              
              if(!
    in_array($job$_POST['job_sought'])){
                return 
    '1';
              }
              
              return 
    'selected="selected"';
            } 
    and see what number it puts into your form. At least then we'll know which of the if conditions are failing.

  10. #10
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI

    sorry to bother you again. i seem to be having an terrible time of it.

    it did not return any values to the form. furthermore, if i don't tick the box in the browser it return an error message. the error reads

    Undefined index: job_sought

    do you think my function for this whole thing is incorrect. is there a better way to make the form return these values


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •