Good morning to you all,
I would like to transfer to my target page an id that I have upstream recovered with GET, in the sending form with a php parameter,
<?php $idEleve = $_GET['id']; ?>
<form method="POST" action="addNotes.php?id=$idEleve">
I’m not sure it’s okay, please help me
There are two options.
1) You can transfer it as a GET parameter to the next page in your form action attribute:
<form method="POST" action="addNotes.php?id=" . <?= $idEleve ?>
Then receive it the same way:
$idEleve = $_GET['id'];
2) Use a hidden input field
<input type="hidden" name="id" value="<?= $idEleve ?>">
In that case it will be a part of your $_POST array:
$_POST['id']
Related
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'];
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 echoing out a form that uses the method GET to send material via several input type="hidden". Like this:
echo '
<form action="somewhere.php" method="GET" name="whatever" id="whatever">
<input type="hidden" name="get_method" value=" . '$foo' . ">
<input type="hidden" name="want_to_send_with_post" value=" . '$my_post' . ">
</form>
';
This works fine, but how do I send one of these input types as POST, since it's the form that determines the method? Is there a way that an individual input type can override the form method? I know that an input button can override the form method (see here), but that's creating a separate entity. I want to send both GET and Post simultaneously.
The reason: I want to send category names with Get and a text message with POST.
Yeah, you can! Specify the GET data in the URL you're heading to.
(I would edit the GET info with javascript as the user inputs their data)
<input type="text" id="get"> <!--user types get stuff here-->
<form action="somewhere.php?get=something" method="POST" id="form">
<input type="hidden" name="post" value="<?= $mypost ?>" />
</form>
Use JS to change the GET action
var form = doc.getId("form")
var get = doc.getId("get").onkeyup = updateLoc;
function updateLoc() {
form.action = "somewhere.php?get=" + get.value;
}
Then PHP can deal with $_POST["post"] and $_GET["get"] separately.
I've a html form for get Id from visitor
<form action="show.php" method="post">
<p>Your id: <input type="text" name="id" /></p>
<p><input type="submit" /></p>
</form>
and the php file for act this .
<?php
$id = ((int)$_GET["id"]);
$con=mysql_connect('localhost','user','pass');
mysql_select_db('dbname',$con);
$query="SELECT * FROM `users` WHERE id = '$id'";
$select=mysql_query($query);
while($row=mysql_fetch_array($select)){
echo $row['name'];
}
mysql_close($con);
?>
but it's not working , can't read id , pelase help me for resolve this issue
Thank you
You are submitting data via POST.
Thats defined by the attribute "method" within your form tag:
<form action="show.php" method="post">
So data will not be stored in $_GET but in $_POST.
So to access the ID use $_POST['id'] not $_GET['id']
As you have used form method "post", id variable will be available in the global $_POST array so use:
<?php
$id = ((int)$_POST["id"]);
....
Use POST intead of Get because use set post method for form.
$id = ((int)$_POST["id"]);
Your form is submitting data via POST so you have to accept it via POST method
<?php
$id = $_POST['id'];
?>
otherwise change the method to GET and write it like this
$id = ((int)$_GET["id"]);
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.