Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using link to submit form into php using $_PHP_SELF and $_POST

Tags:

forms

php

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();">

like image 755
Auz Beauxdine Avatar asked Feb 06 '26 19:02

Auz Beauxdine


1 Answers

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'];?>">
like image 68
Just_Do_It Avatar answered Feb 12 '26 10:02

Just_Do_It



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!