Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts

    using javascript in PHP

    I am trying to get a value out of my form. I have a php value to store it in but to reference the value I need javascript. I tried it a few ways but it didn't work. Any ideas?

    <?php $newName=?> oForm.elements[e].name;

    that is the basic jist but I can't figure out how.


    Jason

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts


    More code please, your request is mildly confusing. What exactly are you trying to do on the server (PHP) and what on the client (JavaScript)? Mixing of these to "script" locations rarely produces the desired output. When PHP has finished processing the file, all output is send to the client and only then may JavaScript do anything, but is incapable of directly invoking PHP functions.

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In my form I used a php to dynamically create a number of check boxes.

    echo "<INPUT TYPE=\"checkbox\" NAME=\"<?php echo $display_time; ?>\" VALUE=\"y\" "
    . (($ver_ans == 1)?"CHECKED":"UNCHECKED") . "> " . "$display_time";

    what I am trying to do is go through after the user makes their selections in the check boxes and input the names of those into a PostgreSql DB for later use. Since I used the form I was trying to access the name using javascript 'cause I know it can, but I can't get the value into php. Does that make more sence?

    Jason

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm still confused. Why don't you submit the form after the user has made his choice (that means, the user submits it of course)? The receiving PHP script can distinguish which checkbox value was selected/checked.

  • #5
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that sounds nice and simple except the part where I get confused. What do you mean by recieveing PHP script? This is all in a popup window. And I still don't get how to figure out which boxes are checked or not. Thanks for all the replies.


    Jason

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, if you have checkboxes in a form, I were assuming that this form would also be submitted, because that's how data in forms usually gets to a PHP script.

    A small example:

    Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="checkbox" name="val1" value="10" /> 10 <br />
    <input type="checkbox" name="val2" value="20" /> 20 <br />
    <input type="checkbox" name="val3" value="30" /> 30 <br />
    <input type="submit" />
    </form>
    
    <?php
    if (count($_POST) > 0) {
        var_dump($_POST);
    }
    ?>
    Getting a checkbox value is pretty simple because every box that has been checked is visible in the $_POST array in the receiving PHP script - that means the page to where the form is submitted. In the above example, we just submit the form to itself, i.e. the same page.
    var_dump is a tremendously helpful method for determining the type and values of a variable, in this case it tells us what variables were submitted by the form.

    Does that help?

  • #7
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that helps alot, but what I don't get now is where the var_dump($_POST) will get displayed on the submit. And that <input type= "submit" /> is that a button, cause I thought I had to decalre it as a button.

    so I have
    '
    document.writeln ( '<INPUT TYPE="button" VALUE="Update" ONCLICK="update_office_hours()">' );
    document.writeln ( '<INPUT TYPE="button" VALUE="Uncheck All" ONCLICK="uncheck_all(this.form)">' );
    document.writeln ( '<INPUT TYPE="button" VALUE="Check All" ONCLICK="check_all(this.form)">' );
    </SCRIPT>

    <NOSCRIPT>
    <INPUT TYPE="submit" VALUE="Update">
    </NOSCRIPT>

    <?php
    if (count($_POST) > 0) {
    var_dump($_POST);
    }
    ?>
    '
    How can I see what is produced in the $_POST array since right now it isn't showing me anything when I do a echo $_POST[0]; or echo var_dump($_POST).
    What am I missing?

    Jason

  • #8
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, I see my error. Now I have a sql problem. Thanks

    Jason

  • #9
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reply to mordred

    I think I figured it all out...thanks mordred

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Jason
    that helps alot, but what I don't get now is where the var_dump($_POST) will get displayed on the submit.
    Right where it is written - var_dump() prints it's message directly to the output.

    And that <input type= "submit" /> is that a button, cause I thought I had to decalre it as a button.
    I would call it a submit button, I don't understand why you are asking if you should declare it as a button... a submit button is a very elementary part of every HTML form... maybe I'm missing sth. now.

    document.writeln ( '<INPUT TYPE="button" VALUE="Update" ONCLICK="update_office_hours()">' );
    Hm. Why are you writing this with JavaScript? Putting it in the HTML source directly would have the same effect plus that people with disabled JS could also use your form.

    How can I see what is produced in the $_POST array since right now it isn't showing me anything when I do a echo $_POST[0]; or echo var_dump($_POST).
    You just write var_dump(), not echo var_dump() - as stated above var_dump() prints directly to the output. As to why your POST variables don't show up there may be a variety of reasons:

    - Wrong URL in action attribute of form tag
    - Form submit method is not POST
    - You run PHP < PHP4.1, in this case you need to use $HTTP_POST_VARS.

  • #11
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, I get array(1) { [""]=> string(15) "" } as my sample output but the string looks blank. Is there a way to isolate the string and print it seperately?

    I also changed my dynamic checkbox creation to :
    echo "<INPUT TYPE=\"checkbox\" NAME=\"<?php $work_day_start_time$min ; ?>\" VALUE=\"<?php $work_day_start_time$min ; ?>\" "
    . (($ver_ans == 1)?"CHECKED":"UNCHECKED") . "> " . "$display_time";


    Jason
    Last edited by Jason; 03-07-2003 at 01:33 AM.

  • #12
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's a rather odd way of putting strings together, it produces input field names like <?php timeValMinVal ; ?>. I think you're doing something wrong here, and rather want this:

    PHP Code:
    echo "<INPUT TYPE=\"checkbox\" NAME=\"$work_day_start_time$min\" VALUE=\"$work_day_start_time$min\" " 
    . (($ver_ans == 1)?"CHECKED":"UNCHECKED") . "> " "$display_time"

  • #13
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks mordred

    Thanks mordred, I think I was getting parse errors so I kept adding <?php ?> crap until I didn't not realizing that I was screwing the code up more. Thanks again, that works and outputs the string contents and everything.

    What is the syntax for the $_POST array? like if I wanted to go through each element?
    Here is sample output
    array(1) { [1745]=> string(4) "1745" }
    and it will always be a string of numbers, how can I get the numbers or the string of numbers?

    Jason
    Last edited by Jason; 03-07-2003 at 10:41 PM.

  • #14
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    K, I got it all...Thanks to he who helped.

    Jason


  •  

    Posting Permissions

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