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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry unable to pass php variable in script function

    hi,
    i am creating two text areas with id's-id11 and id12..what i want is that as soon as user changes the text of any ofthese text areas it should display that text in an alert box.Here is my code:
    --------------------------------------------------------------------------------------------------
    PHP Code:
    <?php
    $i
    =1;
    while(
    $i)
    {
    echo 
    '<textarea id="id1$i" onchange=Display("id1$i")></textarea>';
    echo 
    "id1$i..<br>";
    if(
    $i==2){break;}
    $i=2;
    }
    ?>
    <input name="Submit" type="submit" value="Submit"/>
    <script>
    function Display(id1$i)
    {
    alert(document.getElementById("id1$i").value);
    }
    </script>
    </html>
    --------------------------------------------------------------------------------------------------
    Now when i try running this...the problem is when i enter "one" in the first text area box,it displays "one",that's ok.....but when i enter "two" in the second text area box,it still displays "one"......

    The only reason dat i can think of why it's hapning lyk dat is the function Display(id1$i) is not taking the value of $i=2...please help!!..

    If i use proper names like "id11" and "id12"(i.e i dont use the variable $i anywhere in my code or say if i harcode everythng)..then the code works fine..it displays both "one" "two",respectively..
    Last edited by VIPStephan; 04-10-2013 at 11:50 AM. Reason: added code BB tags

  • #2
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    [QUOTE=bhoots21304;1327212
    i am creating two text areas with id's-id11 and id12..[/QUOTE]

    Well actually, no you are not

    Have a look at the html code your php code is creating and you'll see the id's are not what you say they are.

  • #3
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    o yeah yeah.ok.i sorted out this thing..
    one more thing please.
    i need to insert this value(the one which javascript function shows in an alert box) in a sql database.
    i knw i cannot use javascript to connect to d database,i need to use php for that.
    can u tell me how to do that..just explain,i will write d code..i mean do i need to create a new php file or can i write in the same file, stuff like this....
    btw,thanks for ur help.

  • #4
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    You can do it in the same or separate script.

    Write code to send the data to your php script.

    Then write code to validate the sent data.

    Then sanitise the data.

    Then write your sql query in php to insert the data in the database.

    Then write code to execute the query.

    Fairly straight forward

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    408
    Thanks
    18
    Thanked 26 Times in 26 Posts
    If you want this to be done without the page being refreshed, ave a look into jQuery, it handles all of the data sending/receiving and it's processed with jQuery/javascript after. It's definitely something worth looking into if you want more dynamically updating pages and not the need to refresh the page to send or receive the data.

  • #6
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Quote Originally Posted by Dubz View Post
    If you want this to be done without the page being refreshed, ave a look into jQuery, it handles all of the data sending/receiving and it's processed with jQuery/javascript after. It's definitely something worth looking into if you want more dynamically updating pages and not the need to refresh the page to send or receive the data.
    Bearing in mind that jQuery is nothing more than javascript and not a different language at all, then if you need the functionality to work in 100% of cases then forget javascript or AJAX (which is just a particular usage of javascript/jquery) and do it all server side. If you use javascript/jquery then it may work in 80%, 90% or even 99% of cases but definitely not in 100% of cases.

    So the bottom line is, if it's critical for it to work in 100% of cases, then it must be done server side and not with javascript/jquery.

  • #7
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    you could write it like:

    PHP Code:

    <?php 
    for ($i=1$i<=2$i++){echo "<textarea id='id1$i' onchange=Display('id1$i')></textarea>id1$i..<br>".PHP_EOL;}
    ?>
    <input name="Submit" type="submit" value="Submit"/>
    <script>
    function Display(variable){alert(document.getElementById(variable).value);}
    </script>
    </html>
    Last edited by Arcticwarrio; 04-11-2013 at 12:26 PM.
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #8
    Regular Coder
    Join Date
    May 2012
    Location
    USA
    Posts
    102
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Regarding php code

    I got it. Thanks for your help and sharing the code for providing the alert function.


  •  

    Tags for this Thread

    Posting Permissions

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