user2149578
user2149578

Reputation: 79

PHP form method post with action URL

I have the following problem with my form.

The form looks like:

<form name='add'
      method='post'
      action='<?php echo htmlentities($_SERVER["PHP_SELF"]) ?><?php echo "?naujiena=".$_GET['pavadinimas']."" ?>' >

    <input name='id' type='hidden'>
    <input name='skaicius' type='hidden'>
    <input name='pavadinimas' type='text'>
    <input type='submit' name='prideti' value='prideti'>
</form>

After the form confirmation I see the result in the URL like this:

http://viper.us.lt/php/naujiena/forma.php?naujiena=

It should be like this:

http://viper.us.lt/php/naujiena/forma.php?naujiena=some_value

Upvotes: 4

Views: 22257

Answers (2)

psu
psu

Reputation: 379

Change the form method from POST to GET like this:

<form name='add' method='GET' action='<?php echo htmlentities($_SERVER["PHP_SELF"]) ?><?php echo "?naujiena=".$_GET['pavadinimas']."" ?>' >

Upvotes: 3

Garis M Suero
Garis M Suero

Reputation: 8170

Your approach is wrong. You don't need the <?php echo "?naujiena=".$_GET['pavadinimas']."" ?> on the action attribute.

Just change your method from POST to GET, and after submit (type) button is clicked, you will see the value on the URL, and will be able to get the value as $_GET.


Edited:

Then you need your form like

<form name="add" method="post" id="myForm" action="garissuero.html" onsubmit="changeActionURL()">
        <input name="id" type="hidden" />
        <input name="skaicius" type="hidden" />
        <input name="pavadinimas" id="pavadinimas" type="text" />
        <input type="submit" name="prideti" value="prideti" />
</form>

And have a javascript code like:

function changeActionURL() {
    var forma = document.getElementById('myForm');
    forma.action += "?naujiena=" + document.getElementById('pavadinimas').value;
}

JSFiddle: http://jsfiddle.net/mETwZ/2/

Upvotes: 3

Related Questions