Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
  1. #1
    Junior Member
    Join Date
    Jun 2012
    Location
    India
    Posts
    11
    Member #
    32049
    The app we were given was that in a page there would be one text box with the title at the side 'enter a variable', and there would be a button below it, enabling the user to add more variables in textboxes. The catch is, everytime we hit the Add button the previous textbox would become disabled and still contain the value while a new textbox comes up. I know this involves using the HTML tag "disabled=disabled" and with a counter that does that, but am unable to implement it. Moreover, when I hit the Add button for the first time, two textboxes are added up instead of one, and whenever the page is refreshed a new textbox gets added. Can't figure this out. Been haggling with this for 2 days now.

    P.S. There's an additional element to the page wherein the user enters two variables and the total is displayed. That part is cool. Its only this part that is getting increasingly problematic to fix.

    Here's my code:
    PHP Code:
    <?php
    session_start
    ();
    error_reporting(0);
    ?>
    <?php
        
    if(isset($_REQUEST['total'])){
            
    $val1=$_REQUEST['val1'];
            
    $val2=$_REQUEST['val2'];
        if(
    $val1 && $val2)
            
    $total=add($val1,$val2);
            }
    ?>
    <?php
    /*PHP function Definition*/
    function add($val1,$val2){
    $total=$val1+$val2;
    return 
    $total;
    }
    ?>
    <?php
    $counter
    =0;
        if(isset(
    $_REQUEST['addvar'])){
            
    $_SESSION['counter']=$_SESSION['counter']+1;
            }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Numbers</title>
    </head>

    <body>
    <a href="destroy.php">Destroy</a>
    <form action="" method="post" name="numbers">
    <table>
        <tr>
            <td>Enter 1st number:</td>
            <td><input type="text" name="val1" value="<?php if(isset($_REQUEST['total'])); echo $val1?>"/></td>
        </tr>
        <tr>
            <td>Enter 2nd number:</td>
            <td><input type="text" name="val2" value="<?php if(isset($_REQUEST['total'])) echo $val2?>" /></td>
        </tr>
        <tr>
            <td><input type="submit" name="total" value="Total" /></td>
        </tr>
        <tr>
            <td><input type="submit" name="addvar" value="Add Another Variable" /></td>
        </tr>
        <tr>
            <td>
        <?php
        
    if(isset($_REQUEST['total'])){
            echo 
    "The sum of the numbers are: $total";
            }
        
    ?>
            </td>
        </tr>
        </table>
    </form>
    <table>
        <?php
            
    if(isset($_REQUEST['addvar']))
            {
                for(
    $i=0;$i<=$_SESSION['counter'];$i++)
                {
        
    ?>
            <tr>
                <td>Enter a variable:</td>
                <td><input type="text" name="val" /></td>
            </tr>
            <?php
            
    }
            
    ?>
            <?php
            
    }
            
    ?>

    </table>
    </body>
    </html>

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    You've got a situation here where the scripting you've done is not really even close to what it should be.

    What it needs to do ....
    1) It starts with 2 text boxes and you can add more.
    2) You won't know how many will be added, but you're going to need to know the values of each one.
    3) You'll need to use an array for the text boxes.
    4) You'll need to use an array for the SESSION to contain all of the values they put into the text boxes.
    Again, it could be 10 values, or 25 values ... you won't know. So the SESSION is an array that
    contains the values of all text boxes.

    I don't understand the "disabled" part ... once they enter a value they can't change it?
    Is that what you mean?

    Anyhow, in summary, your script will need to be rescripted from square one.
    The final script will look much different than what you have now. In this situation,
    I'm not going to rewrite the whole thing (not enough time). Maybe you need to
    go back to the teacher and ask for better examples.

    This is a class you're taking, and I'm not sure how your teacher is teaching it.
    It should be taught in sections ... how to use arrays, how to use SESSION, etc.

    You need to know about arrays before you can accomplish your script.


  4. #3
    Junior Member
    Join Date
    Jun 2012
    Location
    India
    Posts
    11
    Member #
    32049
    I have a basic knowledge of arrays and sessions from what our teacher taught us. (Frankly about the teaching part, he's just teaching the very basics and expecting us to get things like geniuses and he's not willing to help much with the examples part. So I'm basically stuck.)

    Alright, I'm getting what you're saying about the arrays and the session declaration.

    About the disabled part, there's an option in HTML as you know which goes something like this if I want to disable a textbox:

    <input type="text" name="textbox" disabled="disabled" />

    I do mean the graying out of the box once the add button is clicked, and yes, once the value is entered, and after the add button is clicked, the value can't be changed again, since the box is disabled. I can't use this directly in the line with the textbox code otherwise the textboxes will come disabled themselves, and that is not what I want.

  5. #4
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    wow ... that's too bad about how the class is being taught VS the level of script he expects.

    Let's write down a flowchart or specifications on what should happen ....

    1) A person is either visiting the page for the first time (no SESSIONS are set),
    or they are revisiting (they have added a value or processed the form at least one time).

    2) If they are visiting for the 1st time:
    You'll always be checking to see if a SESSION was created, so if it does not exist,
    you know they are visiting for the 1st time. In this case, you'll be displaying two
    text boxes. name="value[]" (that creates an array). All of your text boxes will
    have that same name. So they can't add any more values until they have put
    something into the two textboxes that they started with? I'm not sure if that
    specification is true or not. Once they click submit, the textbox array is then
    saved in a SESSION array.

    3) if they have already visited:
    You'll see that a SESSION was created. It will have at least 2 things in the array
    because of step 2. The form displays and the first two text boxes show their
    value, but they are disabled. A new button appears to add more variables.
    They click that and a new one appears. Again ... the specifications? I don't know
    if they can add another variable until they put a number in the new textbox?

    4) You don't need a "total" button because whenever the form is displayed,
    the SESSION array values are added up anyhow. You always know the total.
    You can always display the total. In step 2, the total = 0. Once they get to
    step 3, the total will be whatever it is.

    5) Displaying the form.
    You know what the 'count' is of the SESSION array (how many values it has).
    You'll loop through the SESSION and create each textbox as the loop goes.
    All of them will be 'disabled'. If a SESSION array element contains a value,
    that textbox will be 'disabled'. As you said ... they can't change it anymore.
    When the loop finishes, all textboxes will be disabled and there will be an
    'add new value' button under the form (or under the total if you display that).

    6) Now they are sitting on your webpage as step 3 (they already visited).


  6. #5
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    I got a start on it ....
    Seriously though ... I shouldn't be doing the script for you. You should be doing it.
    I don't want to post the actual script. You need to make a valiant effort first.

    But this is what I have so far to test my script ...
    http://www.catpin.com/test2.php

    EDIT:
    You can't use 'disable' because the textbox value won't be sent to the script.
    So, I am using 'readonly' instead. Same thing as disable, but it gets sent to the script.


  7. #6
    Junior Member
    Join Date
    Jun 2012
    Location
    India
    Posts
    11
    Member #
    32049
    What you did was exactly what I need. Yeah, you're right, I'm trying with the code. And thank you for the direction, I appreciate it. Hopefully following what you said, I will be able to get this done. Will post my script once I manage to get it done. Thank you once again.

  8. #7
    Junior Member
    Join Date
    Jun 2012
    Location
    India
    Posts
    11
    Member #
    32049
    I'm curious about one thing: how do you manage to change the functioning and labelling of the button from add a variable to total the variable?

  9. #8
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    OK ... that part I'll post ... below is the <body> section of the HTML form

    I dynamically generate a different form for each step of the process ...

    I also have another separate SESSION variable for the error message.
    So when it errors and the script re-runs, the error message is retained.

    ... and ... as I said before, this to me seems a bit advanced for a beginning PHP course.
    My script is probably not done the most efficient, and maybe better ways to do it, but it took me
    some time and troubleshooting to make mine work, and I've been doing this a while. Either you're
    really taking a more advanced PHP (not beginning), or the instructor is way off-base.
    Can you describe the course you're taking? Is it specifically PHP, or a "generic programming" class?

    PHP Code:

    <body>
    <a href="<?php echo $_SERVER['PHP_SELF']."?d=d";?>">Destroy</a>
    <form action="" method="post" name="numbers">
    <table>
    <?php
    // are they first time visitors?
    if($flag==1){
    echo
    "<tr>
            <td>Enter 1st number:</td>
            <td><input type=\"text\" name=\"value[]\" value=\"\"/></td>
        </tr>
        <tr>
            <td>Enter 2nd number:</td>
            <td><input type=\"text\" name=\"value[]\" value=\"\"/></td>
        </tr>
        <tr>
            <td><input type=\"submit\" name=\"total\" value=\"Total\" /></td>
        </tr>
    "
    ;
    }
    else{
    // they must have already visited the site, so we'll give them a different form ...
    $c=1;
    $total=0;
    foreach(
    $_SESSION['values'] as $value){
    // let's add each number
    $total=$total+$value;
    echo
    "
        <tr>
            <td>Value 
    $c:</td>
            <td><input type=\"text\" name=\"value[]\" value=\"
    $value\" readonly /></td>
        </tr>
    "
    ;
    $c++;
    }
    // have they decided to add another variable?
    if($_POST['addvar']){
    echo
    "
        <tr>
            <td>New Value:</td>
            <td><input type=\"text\" name=\"value[]\" value=\"\"/></td>
        </tr>
    <tr>
    <td colspan=\"2\">The current TOTAL is 
    $total</td>
    </tr>
    <tr>
            <td colspan=\"2\"><input type=\"submit\" name=\"total\" value=\"Total the New Variable\" /></td>
        </tr>
    "
    ;
    $c++;
    }
    else{
    echo
    "
    <tr>
    <td>The TOTAL is</td>
    <td>
    $total</td>
    </tr>
        <tr>
    <td colspan=\"2\"><input type=\"submit\" name=\"addvar\" value=\"Add Another Variable\" /></td>
        </tr>
        <tr>
    "
    ;
    }
    }
      
    // are there any errors?
      
    if(isset($_SESSION['error'])){
      echo 
    "<tr><td colspan=\"2\"><br />".$_SESSION['error']."<br /></td></tr>";
      }
        
    ?>
        </table>
    </form>
    </body>


  10. #9
    Junior Member
    Join Date
    Jun 2012
    Location
    India
    Posts
    11
    Member #
    32049
    I trust its the second option; its quite possible that he is off-base. The funny thing is, he was teaching us quite well the first few days and normally when you start with a new language there's gonna be stuff that you won't be able to do the first time round and would need help and he keeps on saying that we'll be able to do it and whatever app he's making us do is included in what he's taught us. I'm taking a beginner's PHP course. Yeah, it does seem a bit too advanced for a beginner's doesn't it?

  11. #10
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    The very first lesson he should have taught was to seek help when you're stuck.
    You've done that by posting here. I ask for help too, and I use Google almost all
    the time to get quick refreshers on various PHP/MySQL functions.

    I would be most interested to see what his script looks like. I want to see how he
    scripted the apps (or assignments) he has been giving the student.

    Make sure when this is all over that you ask him for a copy of his script. Tell him you
    want it just to use as a reference (to learn from). Then post it here.

    Meanwhile, I can post my script. It defeats the point of you learning how to do it,
    to find solutions using Google, and of course, the other students may not be getting
    the help. This whole thing sort of irritates me ... not you ... but the idea that if a
    student isn't understanding something, or needs a boost, it's the instructor's responsibility
    to provide that. He claims everything you need for the "apps" has been previously discussed ...
    arrays, session, etc. But what about overall structure, flowcharts, and laying-out what the
    script will be doing? Putting it all together? Getting the brain to operate in the "programmer"
    part ... something like that.

    Find out if other students are having problems.
    No offense, but it is possible that you are not gifted to be a programmer.
    I could take painting classes and have difficulty even though the instructor gave me the tools
    and techniques to paint like a professional. No matter how much help I get, my brain doesn't
    work that way. I can't draw, and I'll never have the gift to draw or paint masterpieces.



Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

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

Search tags for this page

disable textbox in php

,

display values of table in text boxes of form in disabled using php

,

how to disable numbers in textbox php

,

how to disable textbox in php

,
how to disabled textbox can post php
,
php code to disable textbox
,
php disable edit textbox
,
php enabled textbox disabled
,

php textbox array

,

textboxt enable fals in php

Click on a term to search for related topics.
All times are GMT -6. The time now is 12:35 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com