Jump to content

Link as Submit button. A little problem bugging me


irkevin

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.