Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm new in php and i have an issue in creating a form and geting the posted values.
I have two pages login.php and display.php
I need to display in the display page the two values i have inserted on login page using a php function.
>How can I use GET to retrieve my values?
Thanks for your answer.
what i realy whant to do is this:
login page:
<html>
<body>
<form action="./display.php" method="Post">
<h3>
<fieldset>
<?php
echo"<label for='username'>Username:</label>";
echo"<input type='text' name='username' id='username' size='10' title='Username' />";
echo"<label for='password'>Password:</label>  ";
echo"<input type='password' name='password' id='password' size='10' title='Password'/>";
function getvalue(){
// i want to use this function to get the username and password
}
?>
</fieldset>
<br>
<a id="myLink" title="forum" href="./display.php" onClick="getvalue()";return false;">link text</a>
</form>
</body>
</html>
what shoul i do, i know that what i wrote is so incorrect
If your form is something like this:
<form method="get" action="display.php">
<input type="text" name="username" />
<input type="password" name="password"/>
</form>
You can use php in display.php like this:
<?php
echo $_GET['username']."<br />";
echo $_GET['password']."<br />";
?>
If you use this option, your URL would look like:
http://example.com/display.php?username=something&password=somethingelse
This is NOT what you want.
However, I would use POST as it is more secure.
You can do this by changing the method to:
method="post"
and in display.php changing it to:
echo $_POST['username']."<br />";
echo $_POST['password']."<br />";
The URL will look like this then:
http://example.com/display.php
NOTE: Just so you know, do not ever send a password over GET! This is just an example. Anyone who does this is an idiot.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I've created a website where I can upload articles however im having trouble with updating articles that have been created. I have managed to fill in a form with the information from the database pre-filled in but when i submit any changes then to the article then it does not update.
The $var_value is the primary key passed from the previous page to determine which article to load & edit.
Here is my form to update the article.
<?php
$var_value = $_POST['varname'];
$get_studies = "select * from news where `news`.`articleID` = $var_value";
$run_studies = mysqli_query($con,$get_studies);
while($row_studies = mysqli_fetch_array($run_studies)){
$newsTitle = $row_studies['title'];
$newsDate = $row_studies['date'];
$shortBio = $row_studies['shortBio'];
$longBio = $row_studies['longBio'];
$longBio2 = $row_studies['longBio2'];
$image = $row_studies['image'];
}
echo "
<div class='panelContent1' id='addNewsWraper'>
<h2>Dashboard</h2>
<h3>Update Article</h3>
<form method='post' enctype='multipart/form-data' onsubmit='alert('stop submit'); return false;' >
<div class='newsForm'>
<p>Article Title<br /><input type='text' value='$newsTitle' name='newsTitle' /></p>
<p>Short Description<br /><textarea name='newsShort' placeholder='Around a paragraph' />$shortBio</textarea>
<p>Image<br /><input type='file' name='newsImage' /></p>
</div>
<div class='newsForm'>
<p>Date<br /><input type='text' value='$newsDate' name='newsDate' placeholder='2017' /></p>
<p>Story<br /><textarea name='newsLong' placeholder='News article text' />$longBio</textarea>
<p>Story2<br /><textarea name='newsLong2' value='' placeholder='News article text' />$longBio2</textarea>
<button type='submit' name='updateNews'>
Update
</button>
</div>
</form>
</div>
";
?>
Here is how i am trying to update the article. I have tried to update the record based on a primary key, this variable is being passed to the page as its what is loading the content in the form.
<?php
if(isset($_POST['updateNews'])){
$newsTitle = $_POST['newsTitle'];
$newsDate = $_POST['newsDate'];
$newsShort = $_POST['newsShort'];
$newsLong = $_POST['newsLong'];
$newsLong2 = $_POST['newsLong2'];
$newsImage = $_POST['newsImage'];
$insertNews = "UPDATE mods SET title='$newsTitle', date='$newsDate', shortBio='$newsShort', longBio='$newsLong', longBio2='$newsLong2', image='$newsImage' WHERE articleID='$var_value'";
$updateNews = mysqli_query($con,$insertNews);
if($updateNews){
echo "<script>alert('Article updated.');</script>";
}
}
?>
You say $var_value is being passed to your php update script, but I cannot see that is is, nor is it being picked up by a POST and transferred to a local variable. Pass it as a <input type="hidden" and then pick up with a POST to use.
In first php script:
<p>Article Title<br /><input type='text' value='$newsTitle' name='newsTitle' />
<input type="hidden" value='$var_value' name='var_value' />
</p>
In second php script:
$var_value = $_POST['var_value'];
It would also be good to look at protecting your script from sql injection by using a parameterized query.
There is a problem with your query
insertNews = "UPDATE mods SET title='$newsTitle' date='$newsDate' shortBio='$newsShort' longBio='$newsLong' longBio2='$newsLong2' image='$newsImage' WHERE articleID='$var_value'";
Replace this with your current Query you will be good to go.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to retrieve the id of the page but the page is dynamic depending upon the id
e.g filepath/Post.php?id=5
Data is sent through a form action where i want to get the other pages id but i don't know how to do it.
echo "<form action='../PHP/Comment.php' method='post'>" . "<input class='comment' type='text' name='comment' placeholder='Add a comment'>" . "</form>"
//then what the action does
$id = isset($_GET['id']); //trying to get the page id but it doesn't
$comment = $_POST['comment'];
//insertion into the table in the database.
mysql_query("INSERT INTO comments (id, comment) VALUES ('$id', '$comment')")
There are better ways to do this. have you tried using a dedicated $_SESSION variable? In PHP, i cud do this:-
<?php $_SESSION['PAGE_ID'] = 10;?>
then retrieve it on the form as
<?php echo $_SESSION['PAGE_ID']?>
Try this
echo "<form action='../PHP/Comment.php' method='post'>
<input class='comment' type='text' name='comment' placeholder='Add a comment'>
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" value="Submit" />
</form>"
Then form processing
$id = isset($_POST['id']);
$comment = $_POST['comment'];
mysql_query("INSERT INTO comments (id, comment) VALUES ($id, '$comment')");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is page 2
$var= $_GET['var'];
$id= $_GET['id'];
echo "Book Title:<br>" .$var. "<br> Book id:".$id."<br>";
$result = mysqli_query($con,"SELECT * FROM books WHERE bookid='$id'");
while ($row = mysqli_fetch_array($result)) {
echo "<br><br>Author:<br>".$row['author'];
echo "<br><br>Average rating is:<br>" .$row['avgrating'];
}
?>
<form name='myForm' action='addreview.php' method='POST' >
Give your reviews here:<br>
<input type='textarea' name='review' style='width: 500px; height:200px' ><br>
<input type='hidden' id='ids' name='ids' value=''<?php echo $id ?> ''>
<input type='SUBMIT' name='done' value='DONE'>
</form>
on page 1 I am retrieving bookid from database and then passing it to page 2, and then i have to pass it to page 3. on page 3 I am retrieving it with GET command but it is not working. Kindly help me
$_GET and $_POST in PHP correspond to the form method= in the HTML. If your form's method="POST", you must use $_POST to get to the value.
You've got too many quotes:
<input type='hidden' id='ids' name='ids' value=''<?php echo $id ?> ''>
^^-- ^^--
That'll produce HTML that will be interepreted as (for an id of 8):
<input [...snip...] value="" 8 "" />
value gets an an empty string, followed by an invalid attribute 8, followed by a couple quotes that aren't a valid attribute OR value.
If you are submitting page 1 via POST, you will need to access your submitted data using $_POST, not $_GET.
<input type='hidden' id='ids' name='ids' value=''<?php echo $id ?> ''>
Look at your value:
value=''<?php echo $id ?>''
It shoud be
value='<?php echo $id; ?>'
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
In the following code, everything works except that when you click the submit button, nothing happens at all
Please tell me what's wrong, the connection and everything is fine (it is included in the Header.php file) and submit forms on other pages work, but this one seems to just do nothing, page doesn't even load
Thanks!
Here is the code
<?php
//connection is in header
include "../Header.php";
//checks if logged in
if (!$User)
{
header("Location: ../index.php"); exit();
}
//heres the form
echo"<form><center><br /><br /><br /><br /><font size='3'><br />You are changing your Post Color<br />
Your current color is $myU->PostColor<br /><br /></font><form>
Color:<br /><textarea name='color' rows='1' cols='15'></textarea><br /><br />
<input type='submit' name='Submit' value='submit'></form></center></form>";
$Color = mysql_real_escape_string(strip_tags($_POST['Color']));
$submit = mysql_real_escape_string(strip_tags($_POST['submit']));
if ($submit) {
mysqli_query("UPDATE `socialli_main`.`Users` SET `PostColor` = '$Color' WHERE `Users`.`ID` ='$myU->ID'");
header("Location: ../index.php"); exit();
}
include "../Footer.php";
A submit button will submit the form that it is inside. You don't have a <form> element at all. You need to add one.
More importantly than adding a <form> tag is actually setting attributes on it. Try the following.
<form action='' method='POST'>
And you also end your </form> tag many times...
Here is how I would fix up your code anyways:
<?php
//connection is in header
include "../Header.php";
//checks if logged in
if (!$User)
header("Location: ../index.php"); exit();
?>
<form action='' method='POST'>
<div style="text-align:center;">
<br /><br /><br /><br /><br />
<span style="font-size:1.6em;">
You are changing your Post Color
</span><br>
<span style="font-size:1.3em;">
Your current color is <?=$myU->PostColor?>
</span><br>
Color:<br />
<textarea name='color' rows='1' cols='15'></textarea>
<br /><br />
<input type='submit' name='Submit' value='submit'>
</div>
</form>
<?php
$Color = mysql_real_escape_string(strip_tags($_POST['Color']));
$submit = mysql_real_escape_string(strip_tags($_POST['submit']));
if ($submit){
mysqli_query("UPDATE `socialli_main`.`Users` SET `PostColor` = '$Color' WHERE `Users`.`ID` ='$myU->ID'");
header("Location: ../index.php"); exit();
}
include "../Footer.php";
?>
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
table updates empty spaces when user do not enter anything to the textbox
greetings :)
i am having problems updating my database whenever the user clicks on the submit button.
i am going to show you the flow of my program,i already tried figuring out the problem,but i just can't find solutions. i hope someone could help me.
i have 2 problems encountered here:
my database won't update after clicking the submit button
the user may choose which to update,if the textbox is empty,it will update the data with empty spaces.and i want the data to remain as it is if the textbox is empty.
in my program,if you want to update the employee information,you must click the name that contains a link in the page. (in my program its the employee name that needs to be clicked) when clicked,a pop up will open.
the link in my index.php contains the following code:
<td class="sub" width="100" align="center">
<a href="" onclick = javascript:newPopup('empinfo.php?emp=<?php echo $eid ?>');><?php echo$ename?></a>
</td>
NOTE the empinfo.php is my pop up window,it calls the pop up when clicked. emp isthe name i assign to pass in the empinfo.php it contains the employee ID. NO PROBLEM HERE,I JUST WANT TO SHOW YOU THE FLOW
when the empinfo.php appears,it will show this format:
Employee name: //textbox here
Position: /textbox here
Department: /textbox here
Employee Tag: /textbox here
**SUBMIT BUTTON**
when the user clicks the submit button, it should have updated the database with the inputted values,but mine won't update :(
here is the codes i used:
<?php
$con=mysql_connect('localhost','root','mariel') or die(mysql_error());
mysql_select_db('intranet',$con);
if(isset($_POST['submitted']))
{
$qry = "UPDATE gpl_employees_list SET emp_nme = '".$_POST['name']."', emp_pos = '".$_POST['pos']."', emp_dep = '".$_POST['dep']."', emp_tag = '".$_POST['tag']."' WHERE emp_id = '".$_GET['emp']."' ";
mysql_query($qry) or die (mysql_error());
}
?>
this is the content code in my form,together with the submit that i used:
<form action="index.php" method="POST">
<input type='hidden' name='submitted' id='submitted' value='1'/>
<input type='hidden' name='eid' id='eid' value= '<?php echo $_GET['emp']?>' />
<fieldset>
<div class='container'>
<label for='ename' >Employee name:</label><br/>
<input type='text' name='ename' id='ename' value='' maxlength="50" /><br/><br/>
</div>
<div class='container'>
<label for='pos' >Position:</label><br/>
<input type='text' name='pos' id='pos' value='' maxlength="50" /><br/><br/>
</div>
<div class='container'>
<label for='dep' >Department/Division:</label><br/>
<input type='text' name='dep' id='dep' value='' maxlength="100" /><br/><br/>
</div>
<div class='container'>
<label for='tag' >Employee Tag:</label><br/>
<select name="tag" id="tag">
<option value="Y">Yes</option>
<option value="N">No</option>
</select> <br/><br/>
</div>
<div class='container'>
<input type='submit' name='Submit' value='Submit' onclick = "location.reload();window.close()"/>
</div>
</fieldset>
</form>
i hope someone could clear it up for me
MisaChan
It's not updating because you probably need to refer to $_POST['eid'] instead of $_GET['emp'] because you don't have it in index.php like index.php?emp=1. You already have that field so use that:
<input type='hidden' name='eid' id='eid' value= '<?php echo $_GET['emp']?>' />
Also you don't need to do this:
onclick = "location.reload();window.close()"
Type submit reloads the page by default.
Lastly, consider #Sam152's pointers :)
There could be a number of things wrong, but these points should help you debug your script.
Firstly you need to escape your post
variables to ensure things like
apostrophes don't mess up your query,
it's also a security vulnerability.
Secondly, make sure your form action is pointing to the PHP script. Maybe put a print statement at the top of the script to make sure PHP is actually receiving the data.
Then assign the value of the SQL query to a variable and print it out before you run it. You can then easily see what's being sent to the SQL server. Maybe run it in an SQL management tool my phpMyAdmin and observe any errors with it.
Hope this helps. Feel free to update your question with new information as it comes.