Button functioning as link does not work with $_GET - php

I am using a button to function in the same way as a hyperlink:
<form action="intro.html"><input type="submit" value="CLICK HERE TO ENTER" ></form>
This works for static links, but does not work with PHP $_GET arguments.
<form action="wrong_choice.php?stage=0"><input type="submit" value="Wrong Choice!" ></form>
Clicking that will proceed to "wrong_choice.php" but not "wrong_choice.php?stage=0"
How can I fix that?
Thank you

Better to use:
<input type="button" value="Wrong Choice!" onClick="document.location.href('wrong_choice.php?stage=0');" />
If you do not want javascript, add method to form, delete parameter from action and add input with type hidden, which stands for parameter.

Action does not accept query string!
If you want to append data into the form which isn't part of the inputs filled by the user, add inside the <form>
<input type="hidden" name="stage" value="0" />

Action is what you want to do with the information in the form: you want to send the form in a email or send the information to another script to manage or comeback to same script.
If you want pass arguments in the form you should put them in form's fields like that:
<form action="wrong_choice.php>
<input type='hidden' value='0' name="stage">
<input type="submit" value="Wrong Choice!" >
</form>
Thanks

Related

$_POST to a form on another page

I was originally using a $_GET method:
''
Using this works, but i've been told that using a $_POST method is better, less messier and just the preferred choice.
So i'm wondering how i would implement the very same thing as above, but using a $_POST method.
You can submit a form with method post or use an ajax request. In jquery this would look like:
$("#myLinkId#").on('click',function() {
$.ajax({url:'phpFile.php',type:'post',data:{...you data here...}});
});
You need to turn this into a form or use jQuery to trigger an Ajax call on click. Here's a form example:
<form action="editNews.php" method="post">
<input type="hidden" name="starts" value="<php echo $starts?>">
<input type="hidden" name="end" value="<php echo $end?>">
<input type="hidden" name="event" value="<php echo $event?>">
<input type="submit" name="submit" value="Your Link" data-icon="edit">
</form>
To send data via PHP to another page/form, you can do this:
Original form:
<form method='post' action='new-page.php'>
inputs here, etc.
submit button here
</form>
So this will, upon clicking submit, send you to new-page.php with all of the form data held in the PHP variable $_POST which is an array.
You can access the data by referencing the array with the name of the input, for example, say that you had:
<input type='text' name='NAME' />
You can reference this data on new-page.php with $_POST['NAME'].
I hope this helps you!

how to put a form value in to variable in php?

i am doing url masking to pass the values. the code below input a variable.
<form >
<input type="text" name="name" required />
<button type="button" id="btn" action="drive/<?php echo$_GET['name'];?>
/">submit</button>
</form>
i want to pass the value like
drive/12
but when i click the submit button it is passing like
drive/?name=3.
what is the correct way of submitting the value?
The easiest option is to just set the form method to POST.
Otherwise, you'll have to add a javascript handler to the form's submit event, encode the text field and redirect the browser to the desired url.
the form URL is where your action should be and the submit stays the same - right now the NAME is being passed as a get by default for the form.
<form method="post" action="drive/<?php echo$_GET['name'];?">
<input type="text" name="name" required />
<input type="submit" value="submit">
</form>

How to get a form name in php script?

For my php file, I need to grab the unique form name.
The php file is executed when a user clicks the submit button. However, there are multiple submit button each with the same id, but they all have unique names. I need the name when they click on the submit button.
you dont want elements in html with the same id - bad practice in general. Your page will likely load normally but an html validator will notice it as an error.
html validator: http://validator.w3.org/
without seeing your code, its difficult to give you a definitive answer. if you have miltuple forms you can use hidden inputs. e.g.
<input type="hidden" name="form_name" />
Otherwise you can use javascript to put data in the form when the button is clicked. example javascript using jquery
html:
<form id="formid" >
<button type="button" id="someid" onclick="submitForm('btn1')" />
<button type="button" id="someid" onclick="submitForm('btn2')" />
<input type="hidden" id="btnsubmitid" value="" />
</form>
js:
function submitForm(btnID){
$("#btnsubmitid").val(btnID);
$("#formid").submit();
}
1 way is to put a hidden input inside of your form.
<input type="hidden" name="formName" value="[name of form]" />
then in your php, you can get it using
$form-name = $_POST['formName'];
pretty sure there are other ways, but this came to mind first.

How to post form variables with javascript, just like a type="submit" button

I'm a noobie programmer and I wonder how to properly submit a form with javascript.
I made some test code to show you what I mean:
<?php
if (isset($_POST['message']))
{
echo $_POST['message'];
}
?>
<script>
function formsubmit()
{
document.getElementById('form').submit();
}
</script>
<form id="form" name="form" action="" method="post">
<input id="message" name="message" value="hello world">
<input id="submit" name="submit" type="submit">
</form>
Click me<br/>
<input type="submit" onClick="formsubmit()" value="Click me">
When you push the "submit" button inside the tags - the php code will echo "hello world".
When submitting the form with JS the values won't post to the page. What am I doing wrong?
Thanks in advance.
I've searched the whole afternoon for a solution, but cause of my lack of knowledge about programming I failed to find it.
Believe it or not, but the main problem lies here:
<input id="submit" name="submit" type="submit">
If a form contains an input element with name (or id) of submit it will mask the .submit() method of the form element, because .submit will point to the button instead of the method. Just change it to this:
<input name="go" type="submit">
See also: Notes for form.submit()
The smaller problem is here:
Click me<br/>
An empty anchor will just request the same page again before calling formsubmit(). Just add href="#".
The problem here is that the id and name of the input element on your form is called submit.
This will mask the submit function for the form. Change the name and id and you will be able to use javascript to submit the form.
try setting the href of the to '#'. I would guess what is happening is that by clicking on the link, it submits the form and immediately changes the url to the same page you are on cancelling the form submit before it has a chance to go.

Post-variables only passed, when action="" is empty

In a php-script i have a form, method is post, action-attribute is empty, which is working so far. but when i add a value into the action-atribute, like so:
action="index.php?id=9&get-id=5"
the whole post-array is empty after submitting.
Someone has any idea what this could be about?
Thanx in advance, Jayden
edit: here is an Example:
$form = '<form name="form1" method="post" enctype="multipart/form-data" action="index.php?id=9&get-id=5">
<input type="text" name="name1" value="">
<input name="submit" type="submit" value="submit">
</form>';
The form is displayed in a tab in a js-tabmenu, which opens also by get-parameters, in each tab is a form and after submitting the get-param is needed to display the right tab with the right form.
try to use $_REQUEST
which is collection of $_GET and $_POST
You should not use both GET and POST in a request.
You must only use post, therefore the two variables 'id' and 'get-id' should be in the form (use hidden fields)
edit:
try changing your code to:
<form name="form1" method="post" enctype="multipart/form-data"
action="index.php?id=9&get-id=5">
<input type="hidden" name="id" value="9">
<input type="hidden" name="get-id" value="5">
<input type="text" name="name1" value="">
<input name="submit" type="submit" value="submit">
</form>
then if you :
print_r($_POST);
at the top of the index.php page you should be able to see what is going on.
Also - just to check are there any redirects in your code, ie does index.php then redirect somewhere else as that would cause the $_POST to get lost
If you're trying to access id or get-id from your script: Those were appended to the url, even if you submit that form via post. Therefore you will find their values in $_GET, as usual. Only the values of <input> fields (and textarea etc., simply: all form elements) are in $_POST.

Categories