js vars to php

Discussion in 'JavaScript' started by smcblogger, Nov 15, 2009.

  1. #1
    <script type="text/javascript">
    function enter_number(the_box) {
    var new_number = prompt("Enter number", "");
    the_box.innerHTML = new_number;
    }
    </script>

    <td<?php if ($numbers[1][1] == '&nbsp;') {
    echo " onclick='javascript:enter_number(this);'";
    }
    ?>>
    <?php echo $numbers[1][1]; ?>
    </td>

    The cell (td) above uses PHP to decide if the cell has a space or not. If it does, the PHP echoes the js and allows for input. This works fine.

    How do I get the variable that is typed in so that I can work on it in PHP? I'm not looking to use AJAX right now.

    Thanks for any help.
     
    smcblogger, Nov 15, 2009 IP
  2. Wrighty

    Wrighty Peon

    Messages:
    199
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You'd have to submit it via a form. What's the ultimate idea of the script meant to do?
     
    Wrighty, Nov 15, 2009 IP
  3. smcblogger

    smcblogger Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, but how do I access (call) the variable. I'm sorry to say, but I don't know my way around js. Once I can access the var, I can submit it via a form. I'm using to check that all the numbers are present.
     
    smcblogger, Nov 15, 2009 IP
  4. Wrighty

    Wrighty Peon

    Messages:
    199
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I don't understand what you're trying to achieve. You're using a prompt, are you trying to submit that number from the prompt to the PHP ?
     
    Wrighty, Nov 15, 2009 IP
  5. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    ajax or comet. alternatively, set a cookie and read it with php later. heh.
     
    dimitar christoff, Nov 15, 2009 IP
  6. Ascendancy

    Ascendancy Active Member

    Messages:
    1,721
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    90
    #6
    Sounds like you're trying to perform some type of Ajax task, which seems like there are a lot better ways to implement the variables through PHP files and HTTPGetRequest
     
    Ascendancy, Nov 15, 2009 IP
  7. smcblogger

    smcblogger Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ok, maybe I'm making things to difficult for no reason. I suppose the easiest way to do my check is with js, so submitting to PHP is out. Still, how do I call/access the variables?

    the_box.innerHTML[0],
    the_box.innerHTML[1],
    the_box.innerHTML[2], etc.?
     
    smcblogger, Nov 16, 2009 IP
  8. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #8
    As you don't want ajax, then you have to use some form elements. If you don't want to mess up the table with text field, use type hidden, and set action in form to your PHP file. Based on method in the form you can then read the values from e.g. <input type="text" name="the_box_1" value="1" /> like: $num = $_POST['the_box_1']; or $num = $_GET['the_box_1'];
    Then your code may look like:
    
    <script type="text/javascript">
    function enter_number(the_box) {
    var new_number = prompt("Enter number", "");
    the_box.innerHTML = new_number;
    }
    </script>
    
    <form action="read.php" method="post">
    <td><input type="text" name="the_box_<?php echo $order ?>"<?php if ($numbers[1][1] == '&nbsp;') {
    echo " onclick='javascript:enter_number(this);'";} ?> value="<?php echo $numbers[1][1]; ?>" />
    </td>
    ...
    
    <input type="submit" />
    </form>
    
    Code (markup):
    Variable $order represents just some value that will increment for every text field, so you have unique names. Other way is to use name="the_box[]". That will be automatically parsed into array on form submit, and you can read it in PHP like $num = $_POST['the_box']; and in $num is array of all values...
     
    Last edited: Nov 16, 2009
    lp1051, Nov 16, 2009 IP
  9. smcblogger

    smcblogger Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for the help so far guys. Slight change of topic: How can I change a cell's value without using a form or submit button?
     
    smcblogger, Nov 18, 2009 IP
  10. smcblogger

    smcblogger Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Never mind. Figured it out. Thanks guys! =)
     
    smcblogger, Nov 18, 2009 IP