Results 1 to 10 of 10

Thread: filling a php variable from a javascript function

  1. #1
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default filling a php variable from a javascript function

    I am having trouble mixing php and javascript together. What I want to happpen is when a user selects from the drop down the javascript function is run, to grab the new email id. Thanks in advance for the assistance
    PHP Code:
    <?php
    $selected 
    "";
    echo 
    '<select name="email_type" size="1" onchange="emailType(this.value)">';
    echo 
    '<option value="0"> Select...</option>';

    $query "SELECT id, subject FROM table WHERE category = 'Templated Email' ORDER BY `category` ASC";
    $result mysql_db_query($dbase,$query,$id);
    $number mysql_num_rows($result);
    $i 0;
    while(
    $i<$number):

    $email_id mysql_result($result,$i,0);
    $subject mysql_result($result,$i,1);
    echo 
    '<option $selected value='.$email_id.'>'.$subject.'</option>';

    $i++;
    endwhile;
    ?>
    <script type="text/javascript">
    function emailType(){
    if (document.form1.email_type.value == 1)
       {
       <? $email_id 1?>
       }else{
       <? $email_id ?> == document.form1.email_type.value;
        }
    }
    </script>

  2. #2
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    In the JavaScript, you'll want to echo PHP variables. By setting them within the JavaScript code, you aren't doing anything to the JavaScript itself. You can only do that by echoing something. Make sense?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  3. #3
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Code:
    <script type="text/javascript">
    function emailType(){
    if (document.form1.email_type.value == 0)
       {
       <?=$email_id = 1; ?>
       }else{
       <?=$email_id; ?> == document.form1.email_type.value == <?=$selected; ?>;
    	}
    }
    </script>
    (above) Doesnt work
    Is this what you were mentioning? If not please give example

  4. #4
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Ok, now what you posted there is different thing than what you originally posted. Now you're actually echoing something. I'm curious about the first one, though, where you set $email_id to 1...are you trying to echo $email_id.'=1'; or are you trying to set $email_id to 1 in the actual PHP?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  5. #5
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Good question.
    I am setting ($email_id = 1) if the user doesnt select from a drop down of templates

  6. #6
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Ok, so that is totally server side...

    Hmmm, I'm not entirely sure this is possible. See, PHP doesn't read the JavaScript, it's just going to see $email_id = 1...so $email_id will always = 1. See what I'm saying? The JavaScript if/else statements are just seen as text to be echoed, not actual code to be rendered on the server. So you'll have to find some PHP if/else statement to figure out if they didn't select from a drop down of templates.

    Make sense?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  7. #7
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ya that is what I figured, I was just seeing if it was possible another way.

  8. #8
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    No, because the JavaScript is client side and PHP is server side, they can't "talk" to each other. However, there is server side JavaScript, but I've never messed with it.

    http://en.wikipedia.org/wiki/Server-side_JavaScript
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes, but they still have to communicate via HTTP as PHP would.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Yes, that's true. So no, this isn't really possible.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

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
  •