Viewed   69 times

I was trying to call a particular php function in submit of a form both the form and php scripts are in same page. My code is below.(it is not working and so I need help)

<html>
    <body>
    <form method="post" action="display()">
        <input type="text" name="studentname">
        <input type="submit" value="click">
    </form>
    <?php
        function display()
        {
            echo "hello".$_POST["studentname"];
        }
    ?>
    </body>
</html>

 Answers

2

In the following line

<form method="post" action="display()">

the action should be the name of your script and you should call the function, Something like this

<form method="post" action="yourFileName.php">
    <input type="text" name="studentname">
    <input type="submit" value="click" name="submit"> <!-- assign a name for the button -->
</form>

<?php
function display()
{
    echo "hello ".$_POST["studentname"];
}
if(isset($_POST['submit']))
{
   display();
} 
?>
Tuesday, December 13, 2022
4

If the variable you are checking would be in the global scope you could do:

array_key_exists('v', $GLOBALS) 
Thursday, October 6, 2022
3

String characters can be referenced by their offset using syntax like $a[0] for the first character, e.g.

$string = 'Hello';
echo $string[1];  // echoes 'e'

so PHP is recognising that $a is a string; casting your 'b' to a numeric (which casts to a 0), and trying to test isset on $a[0], which is the first character a

Though it should also throw an illegal offset 'b' warning if you have errors enabled

EDIT

$a = 'a';
echo isset($a['b']), PHP_EOL;
echo $a['b'];

PHP 5.3

1
a

PHP 5.4

Warning: Illegal string offset 'b' in /Projects/test/a10.php on line 6
a

PHP 5.5

PHP Warning:  Illegal string offset 'b' in /Projects/test/a10.php on line 6

Warning: Illegal string offset 'b' in /Projects/test/a10.php on line 6
a
Sunday, December 4, 2022
4

You can bind an event handler to the "submit" JavaScript event using jQuery .submit method and then get trimmed #log text field value and check if empty of not like:

$('form').submit(function () {

    // Get the Login Name value and trim it
    var name = $.trim($('#log').val());

    // Check if empty of not
    if (name  === '') {
        alert('Text-field is empty.');
        return false;
    }
});

FIDDLE DEMO

Friday, September 16, 2022
2

I tried the code of William, Thanks brother.

but it's not working as a simple button I have to add form with method="post". Also I have to write submit instead of button.

here is my code below..

<form method="post">
    <input type="submit" name="test" id="test" value="RUN" /><br/>
</form>

<?php

function testfun()
{
   echo "Your test function on button click is working";
}

if(array_key_exists('test',$_POST)){
   testfun();
}

?>
Saturday, October 8, 2022
 
vimal
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :