Launch a php script without form - php

I've never met such approach before, but I'm wondering why nothing happens after clicking the "Say hello" button.
<html>
<head>
</head>
<body>
<button onclick="myscript.php">Say hello</button>
</body>
</html>
<?php
echo "Hello";
?>
It would look very "bombastic" if I used the "form" markup construction in this case.
Could you point out my mistake? Or if my vision is totally hopeless, what are the unconventional ways to run the php script?

Why are you using onclick? onclick executes javascript code.
Just link to the file
Say Hello
If you must use javascript, you have to redirect to that page using something like window.location
Here's a tutorial on redirecting with javascript

Because onclick events evaluate JScript or JavaScript code, not file name.
http://www.w3schools.com/jsref/event_onclick.asp

onclick is a so called Event-Property. That expects javascript code by default.
this example is equivalent to including a js file with the content
test.php
that would raise an error as there is no object called test... You could put it in quotes which would make it a string and solve the problem... at least no error would be raised.
But back to your question: You don't launch a php script do you? with
<form method=GET action="scriptname.php">
<input type="text" name="name" />
<input type="submit" value="submit" />
</form>
you tell your browser to open a php-file with additional informations from the form.
It is of course possible to communicate with a php file in the background, search for ajax in order to do that.

Related

HTML and PHP in one file

I'm a PHP newbie trying to sort some basics out. I have a user-form that leads to a mysql select query, which works fine. Every tutorial I have found so far has the standard form tag, ie: action='script.php' method='post'. This obviously opens script.php in a new tab/window though.
If I don't want to display what's fetched from my db on a different webpage I have to put the html and php in one document together. I didn't think this is how you would really want to do it though.
My specific question is when you want to display stuff on the same page do you just put everything in together within one document and let users hit the submit button?
NO you dont put your php scripts on the same page as your html file/s
Try this link for your reference =)
OR you can put 2 different pages that act as 1 by using INCLUDE FUNCTION
script1.php
<form action="script2.php" method="post" name="myform">
...
<input type="submit" name='submit_button' value="Submit" />
<input
</form>
---------------
script2.php
include 'script1.php';
if(isset($_POST['submit_button']
{.......}
Yeah You can put html and php in single document.
With the help of action.But it not the proper way.
In action you should mention this for writing html and php in same page.
<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>
You can use the same page as Action in form and make condition based on your submit button whthere it is pressed or not.
If it is pressed you can make your Code there for connecting db and do operation like select, insert, update or delete.
e.g.
Your file: script.php
<?php
if(isset($_POST['btnsubmit'])) {
// Do your Operation here...
}
?>
<form action="script.php" method="post" name="myform">
...
<input type="submit" name="btnsubmit" value="Submit" />
<input
</form>
What you can do is simply refer the user back to the form, or another page on your server with the header tag. Inside your PHP script you'd add something similar after your query executes correctly
header( 'Location: ' . $_SERVER['HTTP_REFERER'] ); // Refer to the last page user was on...
Or another URI
header( 'Location: http://some.url/' );
If you really want to do this, here is a way:
<?php
if(isset($_POST)){
//do your php work here
}
?>
<html>
<form method='POST'>
//form elements here
<input type='submit'>
</form>
<!-- other html code -->
</html>
It depends on the length of your code, if the code is too much, then the better way is to include some script file to your parent file. using include() functions, and your perfect answer is yes. just put everything in together within one document

How can an HTML page extract parameters passed through a form?

My question is should I convert two html pages to php pages, so the called page can access its POSTed parameters, or is there a way for a called html (.html extension) page to access posted parameters?
I've been reading that because posted parameters are server-side there is no way for JavaScript to do this being client side. I've seen nothing about one html page accessing
parameters if that .html page was accessed via a POST.
Here is my calling form. The called form, needs access to TransDesc (below), which is a text field.
<script type="text/javascript" language="JavaScript1.2">
// Check where we came from so that we can go to the right spot when the
// form gets posted from outside of our HTML tree.
var PostURL = '/event.html';
</script>
Enter a Donation Amount
<script type="text/javascript" language="JavaScript1.2">
document.write(
'<form name="InvGenPayDonation"
action="'+PostURL+'"
onsubmit="return validateForm();"
method=POST>');
</script>
<p> $ <input type='text' name='Amount' value="0.00">
</p>
<p>In honor of <span style="color: #FF0000">
<input type='text' name='TransDesc' id='TransDesc' value="" >
</p>
<input type="submit" value="Next"> <br /><br />
A static HTML file cannot access variables that have been POST'ed to it. It can't even know they're there as they're sent to the server in the HTTP request, the server then deals with them and sends the HTML page in the HTTP response. They're 'consumed' before the page is even sent to the client.
You could use GET and access them via JavaScript, or configure Apache to server .html files as PHP files instead though.
In my opinion, php is the easiest way to go, and as far as languages go is pretty easy to learn and pretty intuitive.
You'll have to either convert them to PHP or use GET instead of POST, as GET parameters are accessible through window.location.href
Yes, I would recommend converting the pages to php. If you are set on using HTML files you will have to edit your htaccess file to run HTML pages as php.
You can always use ajax to retrieve and send post and get values.
You can retrieve it with js by creating a php file and access those with ajax from your html files.

Accessing MySQL through Javascript via PHP

When any user logs in, I just want to send 2 parameters to PHP and then want to update related DB table. Is it possible in javascript? If possible any sample code or links appreciated.
You can use JavaScript to request a PHP script using XMLHttpRequest, and include some database interaction with MySQL in the PHP script.
AJAX is the key you're looking for.
A javascript framework such jQuery could help you a lot. There's several built in AJAX methods related in the documentation, specially this method.
It is really pretty simple using JQuery, as pedrorezende said. The easiest way would be something like
$.post('sample/path?var1=value&var2=otherValue');
Edit: here is the full code, I believe this would accomplish what you want (untested):
<script type="tex/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js">
</script>
<script type="text/javascript">
$("#login_form").submit(function() {
$.post("login_handler.php", $("#login_form").serialize());
}
</script>
<body>
<form id="login_form">
<input type="text" name="username"></input>
<input type="text" name="password"></input>
<input type="submit" value="login">
</form>
</body>
The first script will load JQuery.
The second script creates the handler and sends the form data to an external PHP file.
Then in login_handler.php you would send your query to MySQL using the $_POST values.

put javascript value into php

I have a form that when the person selects the date I want the value to go into a php function.
I can view the value by using this.form.orderdate.value, but I want to put that value into the php variable $dateChosen
Here is the page so far:
TDM KML Generator
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<script type="text/javascript">DateInput('orderdate', true, 'YYMMDD')</script>
<input name="submit" type="submit" value="get KML" />
</form>
</body>
Javascript is all executing on the client machine, with the exception of an ajax call (which is still browser/client side and is making a request to a server-side function). PHP happens all server-side. In your case, you're trying to do something that isn't possible, because the PHP script is no longer executing by the time the page is rendered in the browser and the javascript is able to execute.

PHP server event handling

I am looking for a way to have like a button in a web page, only when you press it the event handling is a program in a server. Like a trigger to execute a program in the server.
Is there any way to do this?
A PHP script can execute a command on the server using one of the various functions like exec, system, and passthru. It can be a security nightmare, so be careful.
You might want to look into something like: http://php.net/manual/en/book.exec.php
AJAX for the client part (sending notification about the button being pressed), and PHP (function exec() ou system() ).
Like the others have said, exec, system, and passthru can be used for that, but I believe you will need to disable safe mode.
safe_mode = Off
http://php.net/manual/en/features.safe-mode.php
To add on to this answer, you can make a form
Page1.html
<html>
<head>
</head>
<body>
<form action="script.php" method="post">
<input type="submit" />
</form>
</body>
</html>
script.php
<?php
shell_exec("cmd");
echo "done";
?>
Someone correct me if I am wrong here.

Categories