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"]);
Related
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']
In a code below I want to pass variable $user_id to next page using post method suppose my next page is followers.php . The whole code is link itself i want to put post method in it so that on clicking the link, variable is also passed to follower.php . Don't want to use sessions.
<a href='follower.php' style='text-decoration:none;'>
<h2>Followers</h2>
<?php
$checkfollowers = "SELECT * FROM follow_user WHERE user_id='$user_id'";
$resultfollowers = mysqli_query($con,$checkfollowers);
echo mysqli_num_rows($resultfollowers );
?></a>
I guess code for post method is correct:
<form method="get" action="follower.php">
<input type="hidden" name="varname" value="user_id">
<input type="submit">
</form>
So how to put this post method inside the other code so on clicking, follower.php is opened and variable is also passed to this page.
You are using GET method. Change it to
<form method="POST" action="follower.php">
<input type="hidden" name="user_id" value="user_id">
<input type="submit">
</form>
Also change the name to user_id of the hidden field. Now receive as
<?php
$user_id = $_POST['user_id'];
$checkfollowers = "SELECT * FROM follow_user WHERE user_id='$user_id'";
$resultfollowers = mysqli_query($con,$checkfollowers);
echo mysqli_num_rows($resultfollowers );
?>
Note: Above query is vulnerable to SQLI. Better to use Prepared statements.
You are talking about post method but in your code you are using the method GET. You might want to consider to change this into POST. This way the stuff filled out in the form will not enter the URL. (bit more secure). After this you could call the global variable $_POST['user_id']
A link cannot POST data, only GET.
use:
<a href='follower.php?user_id=1' style='text-decoration:none;'>
and in follower.php file:
$user_id = $_GET['user_id'];
I have this simple form.
a.php
<html>
<head>
</head>
<body>
<?
echo "
<form action = 'a2.php' method = 'post'>
<input type = 'text' name = 'name'>
<input type = 'submit' value = 'SEND' name = 'send'>
</form>
";
?>
</body>
</html>
a2.php
<?
$name = $_REQUEST ['name'];
echo $name;
echo "
<form action = 'a2.php' method = 'post'>
<input type = 'submit' value = 'EDIT' name = 'edit'>
</form>
";
?>
How can I keep the value introduced when I click EDIT and i'm back to the first form?
Thanks.
EDIT 2: using hidden inputs
on a2.php just put another <input type="hidden" name="hidden_name" value="{$_POST['name']}" /> after you hit submit on a2.php (BTW for it to go back to a.php, you need to change the form action="a.php" on a2.php), a.php will have a $_POST['hidden_name'], that will contain the value from the first iteration.
EDIT: before you start handling $_SESSION variables, first initiate the session before any html output with a session_start() function.
Use a superglobal like $_SESSION so in your case you would need to fetch the incoming in a2.php $_SESSION['name'] = $_POST['name'] and refer to the $_SESSION['name'] in your a.php. Remember that $_SESSION['name'] will retain the last assigned value until the the session is terminated, I.e. browser window is closed.
You can read more in http://www.php.net/manual/en/reserved.variables.session.php
Also about session_start: http://www.php.net/manual/en/function.session-start.php
1.creating the hidden inputs in forms by using type="hidden"
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 managed to get my API lyrics code working. All I'm facing is a small problem: When a user enters a song name in a textbox and clicks the submit button, I catch the value via getElementById, and then how do I append it with the URL below?
Here's my code:
<?php
//Catches the value of the Submit button:
$submit = isset($_POST['submit']);
if($submit) {
?>
<script type="text/javascript">
var val1 = document.getElementById('val').value;
</script>
<?php
/* The below $res contains the URL where I wanna append the caught value.
Eg: http://webservices.lyrdb.com/lookup.php?q=Nothing Else Matters(Or
what the user searches for)&for=trackname&agent=agent
*/
$res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q='+val1+' &for=trackname&agent=agent");
?>
<html>
<form method="post" action="">
Enter value: <input type="text" name="value" id="val" /><br/>
<input type="submit" value="Submit" name="submit" />
</form>
</html>
Could you please correct me as to where I'm making a mistake in this piece of code, highly appreciate all help in this forum! :)
As far as I understand your question, what you need to do is:
<?php
//Catches the value of the Submit button:
$submit = isset($_POST['submit']);
if($submit) {
$val1 = $_POST['val']; // TO store form passed value in "PHP" variable.
/* The below $res contains the URL where I wanna append the caught value.
Eg: http://webservices.lyrdb.com/lookup.php?q=Nothing Else Matters(Or
what the user searches for)&for=trackname&agent=agent
*/
$res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q=' . urlencode($val1) . ' &for=trackname&agent=agent");
} // This is to end the "if" statement
?>
and then change the form input field to:
Enter value: <input type="text" name="val" id="val" /><br/>
I am not sure if POST accepts id values too!
Remove the javascript it's unnecessary. PHP runs at the server. Javascript at the client.
Then change your PHP code to this:
if(isset($_POST['value'])) {
$res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q=". $_POST['value'] ." &for=trackname&agent=agent");
}