irkevin Posted August 28, 2009 Share Posted August 28, 2009 Ah the title says, I want to have a link acting as a submit button. I made it. Like below: <script type="text/javascript"> function submitForm(type){ for(i = 0;i < post_form.lenght;i++) document.post_form.post[i].value = type; document.post_form.submit(); } <form method="post" action="test.php" name="post_form"> <input type="checkbox" name="post[]" value="1" /> 1 <input type="checkbox" name="post[]" value="2" /> 2 <input type="checkbox" name="post[]" value="3" /> 3 </form> <a href="javascript:submitForm('delete');">Delete</a> </script> But, when using a submit button, you specify the <name> attribute to use it with php. Like below <input type="submit" name="submit" value="submit" /> <?php if(isset($_POST['submit']){ code there } The name submit will be use in php. But when using a link, how do I specify a name to refer it to php? Can someone please help? Link to comment https://forums.phpfreaks.com/topic/172255-link-as-submit-button-a-little-problem-bugging-me/ Share on other sites More sharing options...
ignace Posted August 28, 2009 Share Posted August 28, 2009 You don't and you wouldn't use: if(isset($_POST['submit'])) but: if (!empty($_POST)) Link to comment https://forums.phpfreaks.com/topic/172255-link-as-submit-button-a-little-problem-bugging-me/#findComment-908220 Share on other sites More sharing options...
Jibberish Posted August 28, 2009 Share Posted August 28, 2009 Could you just add a hidden input called submit in the form <input type="hidden" name="submit" value="submit" /> Link to comment https://forums.phpfreaks.com/topic/172255-link-as-submit-button-a-little-problem-bugging-me/#findComment-908222 Share on other sites More sharing options...
ignace Posted August 28, 2009 Share Posted August 28, 2009 Could you just add a hidden input called submit in the form <input type="hidden" name="submit" value="submit" /> Yeah but IMO using if (isset($_POST['submit'])) is bad practice as your code won't be portable. Imagine I have a new project I have the existing PHP code (which I copy-paste to the new project) but the front-end (html) is made by someone else or like irkevin submit my form using an a-element instead of the normal submit button like I used in my previous project. Link to comment https://forums.phpfreaks.com/topic/172255-link-as-submit-button-a-little-problem-bugging-me/#findComment-908224 Share on other sites More sharing options...
irkevin Posted August 28, 2009 Author Share Posted August 28, 2009 Ignace, your solution is working. But lets say, i have different link which will execute different action. How do i tell the php code which link was pressed? Hope that makes sense. lol Link to comment https://forums.phpfreaks.com/topic/172255-link-as-submit-button-a-little-problem-bugging-me/#findComment-908226 Share on other sites More sharing options...
ignace Posted August 28, 2009 Share Posted August 28, 2009 But lets say, i have different link which will execute different action. How do i tell the php code which link was pressed? Then we are talking about a different story and is something like: if (!empty($_POST)) {//make sure that _POST contains something if (!empty($_POST['multifield'])) {//this should be set and should contain something if ('option1' === $_POST['multifield']) { //handle option1 } else if ('option2' === $_POST['multifield']) { //handle option2 } } } However whenever you process forms you should use something like: $requiredFields = array('..'); if (!empty($_POST) && hasRequiredFields($requiredFields, $_POST)) { //something was posted and all required fields are present. } function hasRequiredFields($requiredFields, $array) { $isValid = true; foreach ($requiredFields as $field) { if (!isset($array[$field])) { $isValid = false; break; } } return $isValid; } Link to comment https://forums.phpfreaks.com/topic/172255-link-as-submit-button-a-little-problem-bugging-me/#findComment-908229 Share on other sites More sharing options...
irkevin Posted August 28, 2009 Author Share Posted August 28, 2009 Sorry to say this. But i don't really understand something. $_POST['multifield'] ! What is multifield reffering to and what is option1 reffering to? Link to comment https://forums.phpfreaks.com/topic/172255-link-as-submit-button-a-little-problem-bugging-me/#findComment-908230 Share on other sites More sharing options...
ignace Posted August 28, 2009 Share Posted August 28, 2009 $_POST['multifield'] is just the name of a field (name="multifield") option1 and option2 refer to: "i have different link which will execute different action" Link to comment https://forums.phpfreaks.com/topic/172255-link-as-submit-button-a-little-problem-bugging-me/#findComment-908231 Share on other sites More sharing options...
irkevin Posted August 28, 2009 Author Share Posted August 28, 2009 Oh i see, but the problem is i don't have a clue of how to assign the otpion to the link.. Link to comment https://forums.phpfreaks.com/topic/172255-link-as-submit-button-a-little-problem-bugging-me/#findComment-908233 Share on other sites More sharing options...
ignace Posted August 28, 2009 Share Posted August 28, 2009 Oh i see, but the problem is i don't have a clue of how to assign the otpion to the link.. <select name="multifield"> <option value="option1">Option #1</option> <option value="option2">Option #2</option> </select> Link to comment https://forums.phpfreaks.com/topic/172255-link-as-submit-button-a-little-problem-bugging-me/#findComment-908287 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.