Results 1 to 9 of 9

Thread: Getting return value from onclick()

  1. #1
    Join Date
    Nov 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting return value from onclick()

    Hello everyone,

    I want to add the "n" number of fields entered by the user in the add_record(). I have used prompt() to take the number from user. But when I alert(result); after onclick(), it gives a message "undefined". Can anyone please tell me the correct way to do this.

    Further, I want to add records by using php. How can I get the value entered by the user, so that I can use that value in php script?

    Here is the code I have done:

    <script language="javascript">
    function add_record()
    {
    var no = prompt("Enter how many records to add","1");
    if(no!=null && no!="")
    {
    num = parseInt(no);
    //document.write(num);
    //return num;
    document.getElementById('ver').value = num;
    }
    }
    </script>


    <body>
    <form name="ser" method="post">
    Name : <input type="text" name="name" /> <br />
    Designation : <input type="text" name="design" /> <br />

    <input type="hidden" name="ver" id="ver" value="-1"/>
    <input type="button" onclick="result = add_record(); alert(result);" value="Add Record" />

    <input type="submit" value="Submit" name="submit1" />


    </form>
    </body>

    Thank you.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <script type="text/javascript">
    
    function add_record(){
     var no = parseInt(prompt("Enter how many records to add","1"));
     if(!isNaN(no)){
      document.getElementById('ver').value = no;
      return no;
     }
    };
    
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you John,

    That's working, but further I want to use "no" in php script, in the same form to add "no" number of records to the form. Can you please tell me how can I get the value of "no" in php variable.

    Thank you.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The hidden input 'ver' now holds the value (the alert was just for troubleshooting). PHP may get the value from the form's post data when it is submitted.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Nov 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi,

    Actually, I want that number "no" before submitting the form.

    Basically, I have 2 fields in my form, Name and Address. Suppose a person has stayed previously on more than one address, he should click the "Add Record" button and then with the help of javascript function "add_record()" the "no" number of records (ie. name and address fields) should get added in the form. And after filling the complete information the form is submitted.

    Please can you help me sorting this out?

    Thank you.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    PHP doesn't gather data from a page or alter it once it has been loaded. I'm a still a little unclear what you are proposing, but if you want to update the page in response to user input without submitting it, you need to use client side (generally javascript) code. Once a page is submitted, even if it is to itself as the action of the form, PHP can be used to update its contents. Using javascript to update a page that will be used to update server side data is unwise though, as many users have javascript either unavailable or disabled. Often javascript can be used for such things as long as there is a server side fall back should there be no javascript.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Nov 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    In my form I take user details from the user, ie. name, address, etc. Now there are two options for user:
    1. Submit form
    2. Add records

    If user dont have any previous addresses, he just submit's the form, which is working fine.

    But if the user wants to add previous address details, he needs to click on "Add records" button. After clicking that button, user should be asked for number of records and then according to the number entered, that many records must be added to the form. After filling all the address details (present and previous) he must click on submit button to submit the form.

    I hope that I have made my point clear this time. Could you please help me out in finding a solution for this?

    Thank you.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I still maintain that the best way of dealing with this would be on the server side. OK, if the user submits the form (as in 1 from your previous post), fine. Say he/she wants to add a record or more. This could be in a separate form that submits to the same page with PHP creating the entries for the new info in the main form on submit of the second form.

    I generally don't advise on specifics in PHP though I'm good at making it do what I want and would consider advising you further on this. But my main expertise is in javascript and HTML, so in the end you might be better off taking this to the PHP forum, that is if you understand my concept and are reasonably convinced it is the way to go.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Nov 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you very much John. I think that I should follow the approach you suggested. I will take this to php forum. Once again thank you.

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
  •