I writing php from handling using this page as an example http://www.w3schools.com/php/php_forms.asp
To check if my form data is being passed to my php script, I am using echo statements. I have had no results, despite trying to print both my password and name. I have spent hours inspecting my code, and trying code from different examples, but nothing works.
I have read the stack overflow post form variables not passing to php, but I have confirmed that my php is working correctly, as I am able to print arrays from my database, as well as html paragraphs. Now I am prone to making really stupid mistakes, so it could be something like that, but I have been as thorough as possible.
Here is what I have done. (I have removed everything that is not relavent)
<form action="login.php" method="post">
<div id="name">
editors name: <input type="text" name="name">
</div>
<div id="pswd">
password: <input type="password" name="pwd">
</div>
<div id="submit">
<input type="submit">
</div>
</form>
login.php
<html>
<body>
<?php
echo $_POST["pwd"];
?>
</html>
</body>
Thank you very much to anyone who has any ideas. I really hope I did not forget something really stupid.
So it works now, I have no idea why it would not work before. Anyways that vardump was pretty usefull for at least letting me know what was going on in the background.
Related
I have a simple form in my PHP project using POST method to send data. Everything works well on the most computers, but there is a strange problem on only one of them.
Imagine that this is my simple form:
<form action="controller/save.php" method="post">
<input type="text" name="username">
<input type="submit" value="show">
</form>
And this is my PHP file:
<?php
print_r($_POST);
?>
In all cases, it returns an array containing the value of username variable. But only on one case, it returns
Array()
I have been never seen such behavior and unfortunately couldn't understand what is the issue. May you help me please?
Below is my form- which value i like to update into database.
<form action="#" class="form">
<div class="form__slider">
<div class="form__slider-rating" id="slider__rating"></div>
<div class="form__slider-value" id="form__slider-value"></div>
<!-- <input type="number" class="form__slider-value" id="form__slider-value" value=""> -->
</div>
<input type="hidden" name="movie_id" id="movie_id" value="<?php echo $post_id; ?>" />
<input type="hidden" name="name" id="name" value="<?php echo $userName; ?>" />
<textarea class="form__textarea" name="remark" id="remark" placeholder="Review"></textarea>
<button type="button" class="form__btn">Send</button>
</form>
and in site it was look like this
This blue mark value I need to update into database. How to do that?
Regarding PHP, you need to handle a form.
Firstly, write a code that would handle the form itself, you can do that, by putting some php on top of your page;
if(isset($_POST['form-slider-value'] && $_POST['form-slider-value'] !== null) {
$value = $_POST['form-slider-value'];
//Database handle
}
Of course don't forget to add name 'form-slider-value' to the input.
Ideally there would be classes or similar stuff for that, I'd advise you to use some kind of framework.
Basically. PHP and FORMS works with handling them, once you submit your form with a button submit, it goes to a place where you address it Form action=. Then everything that is written within names will be at $_POST data.
It is not recommended to keep standalone code in one file, with HTML,css,PHP all together.
Hope that answered your question.
As well. This thing is very common to do or use. I advise you to learn about PHP Forms.
https://www.w3schools.com/php/php_forms.asp
And read about constructing a class. Id say do it the proper way.
Learn about frameworks as well, they are modern day time saviors once you learn it.
I know this will be very basic for the majority of you, but I just dont find the information I need.
I started today with HTML/PHP/JavaScript (no experience in any of those 3). My situation is that I have a html form with a input-field and this input field shall be saved to a php-session-variable.
What I achieved is after pushing the submit button getting the variable in the following .php site and then store it in the session variable.
That is super ugly, isnt it? Its like throwing stones over a fence just to run around in the next moment and picking them up again. How can I directly save information into the session fields on a submit?
That IS the right way!
You should throw information at server and let it save it into sessions!
Sessions are not accessible from client side, that's the fence you mentioned!
To be more accurate:
Throwing over the fence:
<form method="post" name="contact" action="">
<label for="author">author:</label>
<input type="text" id="author" name="author"/>
<input type="submit" value="submit" id="submit" name="submit"/>
</form>
Running around and checking for stone:
if(isset($_POST['submit'])) //Stone found
Picking them up and putting them in their place(!):
session_start();
$_SESSION['their_place']=$_POST['author'];
Hope It Helps.
I'm new to web development and have been wrestling with this problem for several hours now, so I've decided to turn to your wisdom. I'm trying to design a little webpage with a database for my wife to store her recipes in, but I'm having trouble getting form submission to work. Here is the code for the webpage where I take the form information in:
<html><body>
Enter the information below to add a new ingredient for use in your recipes.
<form action="add_to_database.php" method="post">
Name: <input name="name" type="text" />
Serving: <input type="text" name="serving" />
Calories: <input type="text" name="calories" />
<input type="submit" />
</form>
</body></html>
And here is some silly code I've been trying to display on the page to see if I can even get the form submission to work:
<html><body>
<?php
$name = $_POST['name'];
echo $name."<br />";
?>
</body></html>
Unfortunately, the page comes back as completely back (after I hit the submit button). What's absolutely baffling to me is that I've copied and pasted the examples from this page into some files and everything seems to work fine. So it seems as though apache and php are working correctly, but I'm messing up somewhere along the way. My apologies in advance if this seems like a stupid question but for the life of me I can't figure it out.
Do you name the other file as add_to_database.php where the form is submitted. Instead you can test on teh same page by removing the add_to_database.php from the form action.
form action="" method="post">
Name: <input name="name" type="text" />
Serving: <input type="text" name="serving" />
Calories: <input type="text" name="calories" />
<input type="submit" />
</form>
and write the php code on the same page as
$name = $_POST['name'];
echo $name;
If this is not working for you. Create a php file named test.php and type phpinfo(); there.
Verify that your page is indeed being processed by PHP - obvious question, but does your PHP file have a .php extension? Do other things like phpinfo() or echo "Test"; work?
Check the error log in /var/log/apache2/error.log or similar (if on Linux, dunno where that'd be on Windows).
Try turning display_errors on in the PHP configuration (this is a good idea only for a development install, not a production server).
a bit of a longshot, but if you can verify that php is pro essing the page. Clean up your html, you are missing the dtd, head, and encoding. . hard to say how a browser is going to interpret a form (missing a name attribute) without these.
I seem to have a bit of a problem here that I can't quite figure out.
So the deal is I have a php script along with an HTML file, in that file I have a few forms with some text boxes and some drop downs. Now the reason I had to go single forms on all of these was because if I did one big one none of them would work when I would hit the submit button. I have no idea... My current code is below, my previous code only had one form and it wouldn't work at all.
<form method="post" id="locationFromPost" name="locationFormPost">
<div class="formRow">
<div class="label">
<label for="locationForm">Current Location:</label>
</div>
<div class="field">
//If post is null go back to value pull originally else echo post
<?php if($_POST['locationForm']==null) $location=$location;else $location=$_POST['locationForm']; ?>
<input type="text" name="locationForm" id="locationForm" value="<?php echo $location?>"></input>
</div>
</div>
</form>
...... and so on.......
<form method="post" id="sexFromPost" name="sexFormPost">
<div class="formRow">
<div class="label">
<label for="sexForm">Sex</label>
</div>
<div class="dropDown">
<select name="sex" id="sexForm" >
//If post is null go back to value pull originally else echo post
<?php if($_POST['sexForm']==null) $sex=$sex;else $sex=$_POST['sexForm']; ?>
<option value="1" <?php if($sex==1){echo"selected='selected'";}?>>Male</option>
<option value="2" <?php if($sex==2){echo"selected='selected'";}?>>Female</option>
<option value="3" <?php if($sex==3){echo"selected='selected'";}?>>Not Specified</option>
</select>
</div>
</div>
</form>
...... and so on.......
<form method="post" id="submit" name="submit">
<div class="formRow"><div id="seperator"></div></div>
<div class="submitButton">
<input type="submit" name="submit" id="submit" value="Save Changes"/>
</div>
</div>
</form>
So thats what the top part of my code looks like, all the div's are for formatting you don't have to worry about those. Now the php code to save the information is below. I should note that the variables you see in the php parts of the code above are retrieved from the Database in the earlier part of the code, thats working fine. What I am having problems with is I want a user to be able to edit their information then just hit the submit button and the information to be saved. What currently happens is, well, absolutely noting. The page will refresh and all the values will go back to what they were when they were pulled, they are not stored in the DB at all, now I checked to make for sure that part of the code works, buy doing some test. It saves the data no problem. What I thin is the problem is the forms them-selfs, if I do everything in one big form it doesn't work... Don't know why. now if I hit enter at the end of each of the forms individually in the browser the data goes in but just that field, sometimes other fields are wiped completely out and a null or empty string is stored. I cant seem to figure this thing out at all.
heres the php to save the information.
<?php
if (isset($_POST['submit']))
{
mysql_query("UPDATE members SET location= '".$_POST['locationForm']."' WHERE usr='" .mysql_real_escape_string($_SESSION['usr']) ."'");
mysql_query("UPDATE members SET location= '".$_POST['sexForm']."' WHERE usr='" .mysql_real_escape_string($_SESSION['usr']) ."'");
........ you know the rest.......
}?>
I am completely lost at why this thing is doing this, now im not the best PHP programmer by any-means, so it could (probably is) be me. If anyone could point out what I would need to do to get a system like that working please let me know
Thanks.
I wasn't clear exactly what your problem was.
However this:
<?php if($_POST['locationForm']==null) $location=$location;else $location=$_POST['locationForm']; ?>
Doesn't look to me like it is going to do much. Try something like this:
<?php
if(!isset($_POST['locationForm'])){
$location=$location;
}
else {
$location=$_POST['locationForm'];
}
?>
(Curly braces are my own personal liking)
Assuming you have $location defined somewhere you didn't show us. This will check if there is NOT a $_POST['locationForm'], and if there is it will use it.
Try placing all the form fields within on form tag and also use mysql_real_escape_string on your variable as well.
you need all the fields that are going to be submitted to any given page to exist in a single form tag. any elements in a different form tag will submit as a set of THAT form tag.
I' assuming that you also want to maybe combine down your updates?
//$_SESSION['usr'] should already be sanitized and safed wherever you handle your site credentials..
//set your submittable values, with a default to an empty string in this case.
$sex=isset($_POST['sexForm'])?mysql_real_escape_string($_POST['sexForm']):"";
$location=isset($_POST['locationForm'])?mysql_real_escape_string($_POST['locationForm']):"";
//single statement, no need to iterate for each field, that's wasted connections.
$sql="update "UPDATE members SET location= '$location' , sex='$sex' WHERE usr='".$_SESSION['usr']."'";