Asked  7 Months ago    Answers:  5   Viewed   34 times

i have a got a form, on clicking the submit button:

  1. I want to do some task in the same file (db task) AND
  2. I want the form data to be sent to test.php with the redirection

here is my code

    <?php
    if(isset($_POST['btn'])){
        //do some task
    ?>
    <script type="text/javascript">
        var e = document.getElementById('testForm'); e.action='test.php'; e.submit();</script>
    <?php
    }
    ?>
<form name="testForm" id="testForm"  method="POST"  >
    <input type="submit" name="btn" value="submit" autofocus  onclick="return true;"/>
 </form>

but not able to submit the form, if i call the javascript code on onClick, it works.what is the problem in this code, Is there any work around for this

 Answers

84

Just echo the javascript out inside the if function

 <form name="testForm" id="testForm"  method="POST"  >
     <input type="submit" name="btn" value="submit" autofocus  onclick="return true;"/>
 </form>
 <?php
    if(isset($_POST['btn'])){
        echo "
            <script type="text/javascript">
            var e = document.getElementById('testForm'); e.action='test.php'; e.submit();
            </script>
        ";
     }
  ?>
Wednesday, March 31, 2021
 
makadev
answered 7 Months ago
72

Rather than using two hidden elements, Why dont you use a single hidden element, something like this?

for example: you could use:

<input type="hidden" name="accessPermission" value=""/>

and depending upon what button user clicks, with the help of javascript you could set this action's value too:

document.form_access.accessPermission.value = "Revoke Access"

and

document.form_access.accessPermission.value = "Grant Access"

and then submit the form!

so in the end, you can just check value $_REQUEST['accessPermission'] in your PHP,

and you will get whatever value you have set for your hidden form element accessPermission:

if its value is Revoke Access you will get $_REQUEST['accessPermission'] as 'Revoke Access'

if its value is Grant Access you will get $_REQUEST['accessPermission'] as 'Grant Access'

So at the end, you can construct your form as:

<form name="form_access" id="form_access" action="" method="POST" class="access">
|
|
| 

<input type="hidden" name="accessPermission" value=""/>

<a id="_access_btt" class="button" href="javascript:document.form_access.accessPermission.value = 'Grant Access'">Grant Access</a>

    <img class="ajaxload" style="display:none;" id="ajaxld" src="images/ajax-loader.gif"/>
    <a id="_revoke_btt" class="button" href="javascript:document.form_access.accessPermission.value = 'Revoke Access'">Revoke Access</a>

    <img class="ajaxload" style="display:none;" id="ajaxld1" src="images/ajax-loader.gif"/>
</form>

And Both the values are set because, its really specifically set by the lines:

<input type="hidden" name="accessaction" value="Grant Access"/>
<input type="hidden" name="revokeaction" value="Revoke Access"/>

Grant Action takes precedence, because that's appears first.

Wednesday, March 31, 2021
 
kmunky
answered 7 Months ago
27

You can do an ajax request, something like:

var from = blablah...;
$.getJSON("encryption.php", {from : from}, function (data)
{
alert(data.to);
});

encryption.php :

<?php
blabla...
echo json_encode(array("to" => site_Encryption($_GET["from"])));
?>

(It's a sample code, don't copy-paste, read some ajax tutorials ^^)

An alternative could be to make the encryption on client side.

Saturday, May 29, 2021
 
Uours
answered 5 Months ago
25

You can do like

HTML in PHP :

<?php
     echo "<table>";
     echo "<tr>";
     echo "<td>Name</td>";
     echo "<td>".$name."</td>";
     echo "</tr>";
     echo "</table>";
?>

Or You can write like.

PHP in HTML :

<?php /*Do some PHP calculation or something*/ ?>
     <table>
         <tr>
             <td>Name</td>
             <td><?php echo $name;?></td>
         </tr>
     </table>


<?php /*Do some PHP calculation or something*/ ?> Means:
You can open a PHP tag with <?php, now add your PHP code, then close the tag with ?> and then write your html code. When needed to add more PHP, just open another PHP tag with <?php.

Wednesday, June 2, 2021
 
PLPeeters
answered 5 Months ago
95

This is a <select> that will submit the parent form

<form method="post" action="#" name="myform">
    <select name="x" onchange="myform.submit();">
        <option value="y">y</option>
        <option value="z">z</option>
    </select>
</form>

All you have to do is give a name to your <form> and add the onchange event to your <select>...


Adam is right. While the example above works perfectly, I would do it like this:

Using jQuery but there are many other options available...

<head>
<script type="text/javascript" src="jquery.js"></script>
<script>
    $(document).ready(function(){
        $('#mySelect').change(function(){
            myform.submit();
        });
    });
</script>
</head>

and the form

<body>
<form method="post" action="" name="myform">
    <select name="x" id="mySelect">
        <option value="y">y</option>
        <option value="z">z</option>
    </select>
</form>
</body>
Monday, August 16, 2021
 
Luciano Selzer
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :