KriiV
KriiV

Reputation: 2020

Page wont submit via post. Using JS to submit via <a href>

I am trying to make my page submit. I dont want to use <input> for styling reasons so I am using an <a href> tag for my submit button and attempting to use Javascript to submit the form.

So I have the form. Note: the form is echoed out via PHP.

<form method=\"post\" name=\"roomSubmit\" >
    <textarea cols=\"40\" rows=\"1\" type=\"text\" name=\"roomName\" id=\"regularInput\"></textarea>
    <a href=\"#\" onclick=\"document.forms['roomSubmit'].submit();\" class=\"button button-small\">Create</a>
</form>

So as you can see the form name is roomSubmit. I need the information in the textarea to be put into a PHP $_POST variable.

When i do: var_dump $_POST; after attempting to submit using that button, it doesn't show anything in the POST array.

Any ideas would be greatly appreciated!

Thank you!

Upvotes: 0

Views: 55

Answers (6)

StackSlave
StackSlave

Reputation: 10627

Progressive enhancement favors styling an input, so @onsy has the right idea. I would have your HTML like:

<input type='submit' class='button button-small' name='sub' id='sub' value='Create' />

Now your CSS can look like:

.button-small#sub{
  display:inline-block; text-decoration:underline; padding:0; border:0;
}

Upvotes: 1

kimbarcelona
kimbarcelona

Reputation: 1136

Hmm.. what PHP version are you using? Mine is working:

<?php
print_r($_POST);
echo"<form method=\"post\" name=\"roomSubmit\" >
    <textarea cols=\"40\" rows=\"1\" type=\"text\" name=\"roomName\" id=\"regularInput\"></textarea>
    <a href=\"#\" onclick=\"document.forms['roomSubmit'].submit();\" class=\"button button-small\">Create</a>
</form>";
?>

Upvotes: 0

CMPS
CMPS

Reputation: 7769

First put the action attribute in the form:

<form attribute="..." method="post" etc...

second change the onclick content to: try this: onclick=\"document.roomSubmit.submit(); return false;\"

Upvotes: 0

Akurn
Akurn

Reputation: 392

Instead of submitting the form through Javascript. Use a

<button type="submit">Create</button>

element instead. (They are easier to style and can look like <a> tags by removing border and background in CSS). Your form also needs an action= attribute.

Upvotes: 1

patsweet
patsweet

Reputation: 1558

You need an action for your form. For example:

<form action='index.php'...

or

<form action='<?=$_SERVER['PHP_SELF']?>'...

Upvotes: 1

onsy
onsy

Reputation: 750

Try

<input type="submit" value="Submit">

Upvotes: 1

Related Questions