coolgamer48 Posted December 30, 2007 Share Posted December 30, 2007 Can you have a button that causes a PHP function to occur? What I'm trying to do is have several sets of fields, but also have a button that you can press to add another field to the form. Can that be done? Link to comment Share on other sites More sharing options...
boen_robot Posted December 30, 2007 Share Posted December 30, 2007 You can have a submit button that sends the form data to PHP + a special value that when present will trigger that PHP function. PHP will then output the same form data it received, but it will also add that extra field because of the special value.You may also use JavaScript to do this on the client side. Ideally do both - JavaScript because it's faster for simple things like this and won't cause a page reload, and simple form submission for users with JS off. Link to comment Share on other sites More sharing options...
coolgamer48 Posted December 30, 2007 Author Share Posted December 30, 2007 You can have a submit button that sends the form data to PHP + a special value that when present will trigger that PHP function. PHP will then output the same form data it received, but it will also add that extra field because of the special value.You may also use JavaScript to do this on the client side. Ideally do both - JavaScript because it's faster for simple things like this and won't cause a page reload, and simple form submission for users with JS off.I realized the submit thing would work, but I wanted to have a separate button. Is the only way to do that with Javascript? Link to comment Share on other sites More sharing options...
boen_robot Posted December 30, 2007 Share Posted December 30, 2007 I realized the submit thing would work, but I wanted to have a separate button. Is the only way to do that with Javascript?PHP is only executed on the server, and thus - when there's a request to the server. A request is triggered only by the user following a link, submitting a form, or with JavaScript's XMLHttpRequest object.So yes. The only way to do something like that without form submission is with JavaScript. Whether you send XHR or use JavaScript itself for the insertion is up to you (though I'd suggest using JS itself rather than sending an XHR for that). Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.