PDA

View Full Version : Getting return value from onclick()



coolest_coder
11-02-2008, 01:38 PM
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.

jscheuer1
11-02-2008, 03:26 PM
<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>

coolest_coder
11-02-2008, 07:17 PM
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.

jscheuer1
11-02-2008, 07:34 PM
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.

coolest_coder
11-02-2008, 07:40 PM
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.

jscheuer1
11-02-2008, 07:50 PM
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.

coolest_coder
11-02-2008, 08:57 PM
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.

jscheuer1
11-03-2008, 12:42 AM
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.

coolest_coder
11-03-2008, 09:22 AM
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.