3

I'm trying to get a form to submit from a link rather than an input submit button.

    <form id="formadd" name="formadd" method="post" action="<?php $_PHP_SELF ?>">

    ...form data 

     <a name="ADD" onclick="document.getElementById("formadd").submit();" href="" class="button primary">Add Camera to Database</a>
     <!-- <input type="submit" name="ADD" value="Add Camera"> --> 

The PHP that gets called looks like so:

<?php 
            if(isset($_POST['ADD'])) {
                $SIGID = $_POST['SIGID'];
                $LOC = $_POST['LOC'];
                $URL = $_POST['URL'];
                $IMG = $_POST['IMG'];
                $LAT = $_POST['LAT'];
                $LON = $_POST['LON'];
                $CAMTYPE = $_POST['CAMTYPE'];



                $sql = "INSERT INTO cam_markers (SIGID, LOC, URL, LAT, LON, CAMTYPE, IMG) 
                   VALUES ('$SIGID', '$LOC', '$URL', '$IMG', '$LAT', '$LON', '$CAMTYPE')";
                $retval = mysql_query($sql);

                if(! $retval ) {
                   die('Could not add data: ' . mysql_error());
                }
                echo 'Added data successfully' . PHP_EOL;
                mysql_close($connection);
            }
?>

The link currently doesn't work, but it works using a normal submit input, so I know the script is okay.

My best guess was that the problem had to do with the fact that $_POST only creates an array from form elements, and since the hyperlink is not a form element, its name identifier is not getting passed.

But, I tried it without the isset() conditionals and just tried to echo the data that was being passed, which did not work. So, that seems to indicate something else is going on.

Any help/workaround is appreciated. Thanks.

EDIT: I got it working by doing this: <a href="javascript:document.formadd.submit();">

4
  • You don't need to specify action if form is being submitted on the same page itself but if you want to use $_SERVER['PHP_SELF'] ; Commented Nov 7, 2016 at 21:38
  • If all you want is a variable to test with isset(), then you can add a <input type="hidden" name="add"> Commented Nov 7, 2016 at 21:50
  • However, you are in danger: stackoverflow.com/questions/60174/… Commented Nov 7, 2016 at 21:51
  • I changed to single quotes but it still isn't working. I'm not sure why not either. Seems like it should work. Commented Nov 7, 2016 at 22:18

5 Answers 5

1
action="<?php $_PHP_SELF ?>"> 

is wrong. You don't need to specify action if form is being submitted on the same page itself but if you want to use $_SERVER['PHP_SELF'] ; so now

action="<?php echo $_SERVER['PHP_SELF'];?>">
Sign up to request clarification or add additional context in comments.

Comments

0

Use a submit button.

If you don't like the way it looks (the most likely reason for you to try to avoid using the right tool for the job), apply CSS.

1 Comment

I realize that's most straightforward way. But my workflow involves using frameworks to quickly prototype, and I'd prefer to not have to go and edit the sass/css if I can help it. Thanks anyway.
0

You should use a submit button.

button {
  background: none!important;
  border: none;
  padding: 0!important;
  font-family: arial, sans-serif;
  color: #069;
  text-decoration: underline;
  cursor: pointer;
}
<button type="submit">your button that looks like a link</button>

Comments

-1

A couple of things... If action is blank, the browser submits to the current page, so

action=""   fixes it.

You can use the PHP_SELF if you like, but it's not getting printed so either

action="<?php echo $_PHP_SELF; ?>"

or, the shorthand syntax

action="<?=$_PHP_SELF?>"

Finally your code looks for $_POST['ADD'] so create a hidden one within your form..

<input type="hidden" name="ADD" value="ADD" >

Comments

-1

The technique you used is correct, you can use a link and .submit(). The only problem I see is the incorrect quote:

<a name="ADD" onclick="document.getElementById("formadd").submit();"
                                               ^       ^

Use single quotes or escape it.

onclick="document.getElementById('formadd').submit();"

1 Comment

Keep in mind this implies that for this to work your final user must have javascript enabled. The styled standard submit button does not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.