PHP/HTML: Saving HTML variables in PHP Session variables - php

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.

Related

Automating a .php command in Delphi via oleobject

I'm relatively new to Delphi, so if the title doesn't match the contents of the question, my apologies. I'm looking to automate a process on a site that submits a form. I believe I've isolated the code from the site which handles this (values edited for account security) which is:
<form action="action.php" method="post">
<p class="center">
<input type="hidden" name="StringOne" value="StringOneB" />
<input type="hidden" name="StringTwo" value="StringTwoB" />
<input type="hidden" name="StringThree" value="StringThreeB" />
<input type="image" src="img/imageone.jpg" />
</p>
</form>
It is the only form on the page. The form is usually submitted via clicking imageone.jpg. The values of the three inputs are generated dynamically. How would I automate submission of this form? I planned to use the internet explorer OleObject. My code currently looks like
procedure TForm1.Button1Click(Sender: TObject);
var
IE: variant;
begin
IE:= CreateOleObject('internetExplorer.Application');
IE.visible := true;
IE.navigate('http://thesite.com');
I have attempted to use the following (each line is a separate attempt):
IE.Document.GetElementByID('StringOne').setAttribute('value', 'StringOneA', 0);
//above won't work because input has no ID
IE.Document.GetElementByName('StringOne').setAttribute('value', 'StringOneA', 0);
//does not appear to be understood
IE.OleObject.Document.forms[0].submit();
IE.OleObject.Document.forms[1].submit();
//does not appear to recognise the form
I've tried countless other lines of code, but none have been fruitful. Can this be done and if so, how? If any code could be explained, it would be helpful, as I am not well versed in OleObject automation. I can ham-handedly get the values from the HTML source, the main focus of the question is simply submitting the form assuming I have the values. Anything else is a bonus. If anything needs to be clarified, just say.

$_GET method sporadically not passing any data.

so I have been building a small website with a content management system I have built using PHP and mysql. What I am trying to achieve is the user needs to be able to edit and delete the announcement posts they make, so I have been using $_GET to get the ID of the post that is to be edited/ deleted and chucked into a form which holds the code to do that.
Now, this is the code I have been using
if(isset($_GET['edit'])) {
header('Location: announcement-edit.php?AnnouncementID='.$_GET['AnnouncementID']);
}
The form later down the page to execute this is as follows
<form action="announcements.php" method="get">
<input type="hidden" name="AnnouncementID" value="<?php echo $row['AnnouncementID'];?>" />
<input name="edit" value="Modify" type="submit">
<input name="delete" value="Delete" type="submit" >
</form>
This is where it confuses me, this code worked absolutely perfectly then all of a sudden last week it stopped, 2 days ago it started working again and now broken again. When the button is clicked the URL returns as;
url/folder/announcements.php?AnnouncementID=7&edit=Modify
When its supposed to return;
url/folder/announcement-edit.php?AnnouncementID=7
If i manually type the above in the url bar it works absolutely fine and I can update the rows in the DB.
Im totally stumped as to why it is so temperamental, Have I missed something out? am I trying to do this an out dated way?
Its not like I have just followed a random person on youtube how to do this I was actually shown this way at university (only last academic year) and we was told it was "good practice". Seems very weird it works perfectly one minute then refuses to work the next.
Any thoughts or suggestions will be greatly appreciated.
You have two submit buttons in your form, which leads to triggering the submit event only once, when you press Enter key. So, when the Delete button is pressed, your GET parameters will have only delete entry and not the edit entry.
One way of tackling this is, give the same name to both and give a different value, as already there is a different value.
<form action="announcements.php" method="get">
<input type="hidden" name="AnnouncementID" value="<?php echo $row['AnnouncementID'];?>" />
<input name="action" value="Modify" type="submit" />
<input name="action" value="Delete" type="submit" />
</form>
You dont need to use form. You can do it simply using url and use tag
Try to pass your parametr via get parametr like this:
Edit
Delete

php form handling based on w3school example not passing variables

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.

PHP Script won't load

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.

how to Pass Parameters to php page without having to load it

how can i pass parameters from an html page(map.html) to a php(createxml.php) without having to open the php page? Im incorporating googlemaps in html page(map.html) so i want the users to enter data on a form on the html page which will be sent to php(createxml.php) which in turn will connect to mySQL DB and create an xml format of the response the html page uses this xml output to create positions on the map since it contains longitude and latitude.
I have used the following code in the heading of the php page(createxml), but it shows the contents of php file for a brief moment redirecting me to map.html
Thanks for your time, i can post all the code if needed.
<meta http-equiv="refresh" content="0;url=http://localhost/map.html/">
It's quite simple with AJAX, using jQuery you don't have to know much about it :)
So simply import the latest jQuery Library.
Then you have your form:
<form id="my_form">
<input type="text" name="param1" />
<input type="text" name="param2" />
<input type="hidden" name="action" value="do_stuff" />
<input type="submit" value="Submit" />
</form>
and somewhere beneath that, you just paste this tiny javascript-function, which handles the submit of the form:
<script>
$('#my_form').submit(function(){
var post_params = $('#my_form').serialize();
$('#waiting').show();
$.post('the_page_you_are_on.php', post_params, function(data) {
$('#waiting').hide();
return false;
})
});
</script>
(The element (div, p...) with the id "waiting" could e.g. contain one of those fancy ajax loading images, but is not neccessary! :) If you want one to be shown, find one via google, set it as the background image of the #waiting-element and set its display to none (CSS)).
The function itself just calls the page you're on and then you've got the form variables in your post-array, so the top of your page could look something like this:
<?php
if(isset($_POST['action'])) {
switch($_POST['action']) {
case 'do_stuff' :
$param1 = $_POST['param1'];
$param2 = $_POST['param2'];
//do some DB-stuff etc.
break;
}
}
?>
I hope that helps!
It's a terrible idea, but because you don't want to use AJAX you could put the PHP in a frame and reload just that portion. Again, awful idea, but the closest you're going to get without using AJAX.
On a useful note though, AJAX is literally just one function in javascript. It's not hard at all to learn.
If you are just trying to pass parameters to a PHP page from the web browser, there are other ways to do it beyond 'Ajax'. Take a look at this page and view the source code (be sure to view the source of the included javascript file: http://hazlo.co/showlist.php?s=chrome&i=4e289d078b0f76b750000627&n=TODO
It uses an extremely basic method of changing the src of an image element, but passes information to the web server (PHP page) in the querystring of the image request. In this example I actually care about the results, which are represented as an image, but it sounds like you are just trying to pass data to the server, so you can return a 1 pixel image if you like. BTW, don't be fooled by the URL that is being used, a server rule is telling apache to process a specific PHP file when check it,GIF is requested.
You should play with it and use firebug or chrome's built in debugger to watch the requests that are being sent to the server.
You can't get any results from a PHP-script if you don't request it and process the output. If you dont't want to leave the current page, you have to use AJAX!
"but it shows the contents of php file for a brief moment" The reason is, that your browser first needs to load the entire page, then start the META-redirect. You don't need a redirect to load data from the server, but if you really want to, you should HTTP-headers for redirect.
Ok guys after hours of headache i finally found the solution! Basically i called my xmlproduce.php from inside my map.html, lemme explain so maybe will help others:
maps.html contained a googlmap API Javascript function which called my createxml.php called second.php
GDownloadUrl("second.php", function(data) )
what i did was i tweaked this call to second.php by adding some parameters in the URL like:
GDownloadUrl("second.php?strt="+ysdate+"/"+msdate+"/"+dsdate+"&end="+yedate+" /"+medate+"/"+dedate+"&id="+ide, function(data)
which is sending the parameters needed by second.php, so after that i made a small html form which called the script of googlemap api on the same file(map.html) to pass the parameters from the form to the GDownloadUrl i mentioned above, the code of the form is :
form method="get" action="">
IMEI: <input type="text" id="id" name="id" size="25" /> <br />
Start Date: <input type="text" id="ysdate" name="ysdate" size="4" value="2000" /> <input type="text" id="msdate" name="ysdate" size="1" /> <input type="text" id="dsdate" name="dsdate" size="1" /> <br/>
End Date: <input type="text" id="yedate" name="yedate" size="4" /> <input type="text" id="medate" name="ysdate" size="1" /> <input type="text" id="dedate" name="dedate" size="1" /> <br/>
<input type="button" value="submit" onClick="load()" />
</form>
afterwards i put extra constraints on the form for the values allowed.
Thanks everybody for the help, and you can always ask if somebody needs some clarification.

Categories