I have a simple form which was working and now im finding the post data isnt being sent and i can't see the problem
<form role="form" name="challengeform" action="scripts/arena_setup.php" method="POST" onsubmit="return confirm('Are you sure you want to attack this player?');">
<input type="hidden" name="member_id" value="<? echo $member_id;?>">
<input type="image" src="img/map/attack.png" alt="Attack" />
</form>
which is being handled by
if(isset($_POST['challengeform'])){
...
}else{ echo 'error'; }
it always shows the error due to the post data being missing but i just cant see what i've done. Any ideas?
if(isset($_POST['challengeform']))
Form names are not part of the POST data. Only fields within the form.
Try testing for the field itself
if(isset($_POST['member_id']))
You shouldn't write the name of the form. Just write input's name to get the data. Ex:
$var = $_POST['member_id'];
Related
I'm a newbie in PHP, and I would like to send datas from a form and display it into the same page, here is my code for better understanding:
<form method="post" action="same_page.php">
<input type="text" name="owner" />
<input type="submit" value="Validate" />
</form>
<?php
if(isset($_GET['owner']))
{
echo "data sent !";
}
?>
So normally, after having entered some random text in the form and click "validate", the message "data sent!" Should be displayed on the page. I guess I missed something, but I can't figure out what.
You forgot to add submit name in your form.You are using POST as method so code should be
<form method="post" action="">
<input type="text" name="owner" />
<input type="submit" name="submit_value" value="Validate" />
</form>
<?php
if(isset($_POST['submit_value']))
{
echo '<pre>';
print_r($_POST);
}
?>
Will display your post values
You are using a POST method in your form.
<form method="post" action="same_page.php">
So, change your code to:
if (count($_POST) && isset($_POST['owner']))
Technically, the above code does the following:
First checks if there are content in POST.
Then, it checks if the owner is set.
If both the conditions are satisfied, it displays the message.
You can actually get rid of action="same_page.php" as if you omit it, you will post to the same page.
Note: This is a worst method of programming, which you need to change.
You should Replace $_GET['owner'] with $_POST['owner'] as in your form you have specified method='post'
Replace:
$_GET['owner']
With:
$_POST['owner']
Since you are using the post method in your form, you have to check against the $_POST array in your PHP code.
I'm trying to add a value to $_POST data while it gets submitted to the target page as follows:
post.php
<?php $_POST['field1'] = "Value1";?>
<html>
<head>
</head>
<body>
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
catch.php
<?php
foreach ($_POST as $key => $value) {
echo $key . " : ". $value;
echo "<br/>";
}
?>
but I cannot catch 'field1' on the other end. I don't want to use a hidden input field. How can I achieve that?
Thanks!
When you send the form, the $_POST data is reset and assumes only the inputs inside the form and a possible query string you may have appended to form action.
The best way to accomplish what you want is using hidden field but since you dont want it, you can append a query string to your form action:
<form method="post" action="catch.php?field1=Value1">
You're not submitting field1 anywhere. What happens is this:
post.php generates a HTML page (one that doesn't contain any reference to field1)
the user's browser renders the page
on submit, only the elements inside the form are submitted
catch.php receives the elements submitted above.
In other words, you need to get that value into your form:
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input name="field1" type="hidden" value="<?php echo htmlspecialchars($_POST['field1']) ?>"/>
<input type="submit" value="Submit" />
</form>
There is no other way to get the value into your POST data, if it's not present in the form. What you could do as a workaround is store the data in GET (size limit), session (concurrency issues - what happens when the user has two tabs open, each with different session data?), or cookies (size limit AND concurrency issues).
You can't do it this way. If you want to send the data you're trying to add to the POST there only through the users form, you are forced to also store it somewhere client side (e.g. a hidden field or a cookie). What you're doing right now is setting some value into the POST variable, but it gets overridden by the users form post (or rather the $_POST variable you're using after the form post is another instance).
What you could do instead to keep it serverside is save the value in the variable to the session of the user, then in the form post action server side get the value again (given the fact that you're using sessions). Lastly you could just store it in some table in a database, though I wouldn't do this.
Since $_POST are data sent by post method to script, you can not use it for another request directly. You need to compose and send another post request. The easiest way for you will be to use hidden input field/s.
Or you can choose another approach to make http post request, for example curl methods.
If you don't need data to be given by post method, you can save it in session, for example.
try this:
in post.php
<?php $_SESSION['field1'] = "Value1";?>
<html>
<head>
</head>
<body>
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
in catch.php
<?php
if(isset($_SESSION['field1']))
{
$_POST['field1'] = $_SESSION['field1'];
unset($_SESSION['field1']);
}
foreach ($_POST as $key => $value) {
echo $key . " : ". $value;
echo "<br/>";
}
?>
Make sure you have started the session.
Note: you must use hidden elements or query string as other user suggested.
I'm sorry to repeat this question, but the thing is that I have done everything and nothing works. My problem is that I'm trying to pass variables to a second page and it won't work.
Page 1:
<form method="post" name="form1" id="form1" enctype="multipart/form-data" action="editempresas3.php?name=<?php echo $name;?>&descr=<?php echo $descr;?>&dir=<?php echo $dir;?>&pais=<?php echo $pais;?>&tel=<?php echo $tel;?>&fax=<?php echo $fax;?>&email=<?php echo $email;?>&url=<?php echo $url;?>">
<?php
$name = $_POST['empname'];
.....etc
?>
<input name="empname" type="text" required id="empname" form="form1">
.....etc
<input name="submit" type="submit" id="submit" form="form1" value="Crear">
Page 2:
The link will come without the variables
http://www.sample.org/editempresas3.php?name=&descr=&dir=&pais=&tel=&fax=&email=&url=
you should use GET method to achieve this.
change
<form method="post" name="form1" id="form1" enctype="multipart/form-data" action="editempresas3.php">
to
<form method="GET" name="form1" id="form1" enctype="multipart/form-data" action="editempresas3.php">
P.S: if you're form is not uploading anything you can't even miss enctype="multipart/form-data"
Possibilities, from most to least desirable:
Use sessions:
Page 1
session_start();
$_SESSION['var_for_other_page'] = 'foo';
Page 2
session_start();
$myvar = $_SESSION['var_for_other_page']
Use hidden fields:
<form action="secondpage.php" method="post>
<input type="hidden" name="var_for_other_page" value="foo" />
</form>
Put the get vars into the action URL:
<form action="secondpage.php?var_for_other_page=foo" method="post>
<input ... />
</form>
In this case you will have variables in both $_POST and $_GET.
Do not use either 2 or 3 to pass sensitive information.
If you want to send data from a form to a new page, firstly I think your should always use POST. The reason it is not working is you are attempting to send form data via POST but in your action you are trying to build a GET using PHP variables echoed in the there.
e.g.
action="editempresas3.php?name=<?php echo $name;?>&descr=<?php echo $descr;?>&dir=<?php echo $dir;?>&pais=<?php echo $pais;?>&tel=<?php echo $tel;?>&fax=<?php echo $fax;?>&email=<?php echo $email;?>&url=<?php echo $url;?>"
This can't work because PHP needs to process it before the HTML is rendered to print the variables you have chosen.
If you change your action to
action="editempresas3.php"
You will be successfully sent to the next page and if you then use
var_dump($_POST);
On your next page editempresas3.php you will get an output of all fields completed in the page 1 form.
i have a page where i am getting the ques id and insert it in the database for that i am doing
url: */faq/faq_question_sol.php?ques= 62*
this ( $selected_ques= ($_GET['ques']); ) is working properly in the *faq_question_sol.php* but the *answer_submit_process.php* does not recognize it
my form
<form id="post-form" class="post-form" method="POST" action="answer_submit_process.php">
<input id="submit-button" type="submit" tabindex="120" name="submitbutton" value="Post Your Answer" />
</form>
and the *answer_submit_process.php* is
if(isset($_POST['submitbutton'])){
$userid = $_SESSION['userid']; // i have already started the session
$selected_ques= ($_GET['ques']);
$content = $_POST["content"] ;
$query="INSERT INTO `formanswer`( `user_id`,`questionid`,`content` ) VALUES ('{$userid}','{$selected_ques}','{$my_html}' ) ";
$result=mysql_query($query);
}
Quickest solution would be saving the value of $_GET['ques'] on a hidden field of the form and thus make it accessible in answer_submit_process.php.
Something like this:
if (isset($_GET['ques'])){
echo '<input type="hidden" name="ques" value="'.$_GET['ques'].'">';
}
And in answer_submit_process page the value could easily accessed by $_POST['ques']..
If you are sending via a form POST, then the variable from which you can get data is $_POST instead of $_GET.
Anyway, i wasn´t able to find any field relating to the ques variable on your form, where are they?
Add <input type="hidden" name="ques" value="<?php echo $_GET['ques'] ?>"/> to your form to temporarily store the variable, and then use the variable $_POST['ques'] in place of $_GET['ques'] in the processing page.
Alternatively, you could change the form action to answer_submit_process.php?ques=<?php echo $_GET['ques']; ?>.
I am just trying to learn PHP and want to get the value of the textbox using $_post function, but its not working. I am using wamp 2.1 and the code is simple as below
<form method="POST" action="c:/wamp/www/test/try.php">
<input type="text" name="nco" size="1" maxlength="1" tabindex="1" value="2">
<input
tabindex="2" name="submitnoofcompanies" value="GO"
type="submit">
</form>
<?php
if (!isset($_POST['nco']))
{
$_POST['nco'] = "undefine";
}
$no=$_POST['nco'];
print($no);
However in no way I get the value of the textbox printed, it just prints undefined, please help me out.
You first assigned the word "undefine" to the variable $_POST['nco'].
You then assigned the value of the variable $_POST['nco'] (still "undefine" as you stored there) to the variable $no.
You then printed the value stored in the variable $no.
It should be clear that this will always print the word "undefine".
If you want to print the value of the textbox with the name nco, fill out the form with that textbox, and in the page that process the form,
echo $_POST['nco'];
...is all you do.
You need to setup a form or something similar in order to set the $_POST variables. See this short tutorial to see how it works. If you click the submit button, your $_POST variables will be set.
what for you are using this line $_POST['nco'] = "undefine"; } ..?
and please cross check whether you are using form method as post and make sure that your text name is nco ... or else use the below code it will work.
<?php
$no = $_POST['nco'];
echo $no;
?>
<form name='na' method='post' action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type='text' name='nco'>
</form>
thanks
Your action is wrong.
Change it to
action="try.php"