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 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Javascript return value in PHP variable

    What I wanted to do is to call a Javascript function and put the return value into a php variable, is this possible?

    Like this:

    PHP Code:
    echo '<script type="text/javascript">';
        
    $val 1;
        
    $agree 'confirm("Validity: ' $val '\nAre you sure?");';
        
        if(
    $agree) {
            ...
        } else {
            ...
        }
    echo 
    '</script>'
    Thanks for your help and time!
    'x.

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I've asked around and the only solution is by sending the data from a javascript function and get or post it from the PHP script. I'll post my solution here later when I've implemented it.

    'x.

  • #3
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Your only solution is to use a POST or GET send to the php script in question. Javascript is client side, PHP is server side and they're separate for a good reason.
    What you're doing can be achieved in different ways, including a 'splash' page in the php itself, or another way in Javascript which on confirm, redirects to a page, on false it stays on the current.

  • #4
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Ok, what I've done is basically taking option 2 from BluePanther
    Here it is:

    page1.php:
    PHP Code:
    <head>
        <
    script type="text/javascript">
            function 
    validateForm() {
                if(
    document.forms["addForm"].cmp_val.value == "") {
                    
    alert("Validity can't be empty. Please select a validity.");
                    return 
    false;
                }
                return 
    true;
            }
            
            function 
    sendData() {
                if(
    validateForm()) {
                    var 
    val document.getElementById("cmp_val").value;
                    var 
    sup document.getElementById("cmp_sup").value;
                    var 
    agree confirm("Validity: " val "\nSupplier: " sup "\n\nAre you sure the information is correct and that you want to insert it into the database?");
                    
                    if(
    agree) {
                        
    window.location.href "page2.php?cmp_val=" val "&cmp_sup=" sup;
                    }
                }
            }
        
    </script>
    </head>
    <body>
        <form name="addForm" onsubmit="return false;">
            <table>
                <th>Component</th>
                <tr>
                    <td>Validity: </td>
                    <td>
                        <select name="cmp_val" id="cmp_val">
                            <option value="">Choose a validity</option>
                            <option value="Cancelled">Cancelled</option>
                            <option value="Obsolete">Obsolete</option>
                            <option value="Pending">Pending</option>
                            <option value="Valid">Valid</option>
                            <option value="Expelled">Expelled</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Supplier: </td>
                    <td><input id="cmp_sup" class="textfield" type="text" name="cmp_sup" /></td>
                </tr>
                <tr>
                    <td><input type="button" value="Cancel" onclick="window.location.href='homepage.html'" style="cursor: pointer;" /></td>
                    <td><input class="input" type="submit" value="Add" onclick="sendData()" style="cursor: pointer;" /></td>
                </tr>

            </table>
        </form>
    </body> 
    homepage can be anything from index.html to whatever you use as your homepage.
    Next is page 2, the page that will do everything that has to be done when the form is submitted.

    page2.php:
    PHP Code:
    $val $_GET['cmp_val'];
    $sup $_GET['cmp_sup']; 
    The rest of page2.php is up to you, what I did was checking the form input some more, store the data in the database, an automatic redirect back to the homepage and giving a signal to the homepage so that the homepage will show the data the user just entered.

    Automatic redirect:
    PHP Code:
    echo '<script type="text/javascript">';
        echo 
    'setTimeout("window.location.href=\'homepage.php\'", 3000);';
    echo 
    '</script>'
    The sign is a session variable I set like this:
    PHP Code:
    $_SESSION['directSearch'] = $sup
    And retrieve like this:
    PHP Code:
    <script type="text/javascript">
        function init() {
            <?php
                
    if(isset($_SESSION['directSearch'])) {
                    
    // Do something with the passed variable (in my case the supplier)
                
    }
            <?
    php
        
    }

        
    window.onload init;
    </script>
    I did it in like half an hour from scratch, so it's very easy
    Hope I helped anyone besides just me :P
    Enjoy!

    'x.


  •  

    Posting Permissions

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