How to run a php funtion in a html file? [duplicate] - php

This question already has answers here:
Call PHP function from url?
(14 answers)
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
I want to do operation to a database that is running on xampp in my index.html I have the following code
<script src="oPBD.php" async defer></script>
<h1>This is a title</h1>
<form action="opBD.php">
<input type="submit" value="Do stuff" name="Button">
</form>
And this works fine, howevewer I would like to put all the code in opBD.php in a function called function1 for say something and run in html when needed. But How I do that.
I would like to use something like:
<script src="oPBD.php" async defer></script>
<h1>This is a title</h1>
<form action="opBD.funtion1().php">
<input type="submit" value="Do stuff" name="Button">
</form>
I don't find how to do this. Could you guys help me?

Can you just create a new file opBD.functionName.php and then use that in your form action? Otherwise your only real option is to use an MVC (Model View Controller) framework like Laravel or similar which would let you create real URLs for this kind of thing (eg. /user/update or something).

Related

<<<_END What exactly does this do? [duplicate]

This question already has answers here:
PHP heredoc syntax
(3 answers)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 1 year ago.
I'm a college student trying to do one of the exercises from the book. The exercise is typed below. I typed it in exactly as my textbook said too. What is the purpose of <<_END at the beginning and _END; at the end? When I did it this way my html didn't work correctly. When I did it like the instructor did in his lecture it worked. What he did was put all PHP in separate sections . What is the best way to use PHP, HTML5, JavaScript and CSS3 all in one page? Or should I be linking my HTML page to external JavaScript, CSS3, and PHP pages?
My end goal for this assignment is to build a login page and a create a new account page that is linked to my database.
Any and all tips/help is very much appreciated!!
<?php //from the book page 266
echo <<<_END
<html>
<head>
<title>PAGE 266, 267</title>
<body>
<form method="post" action="formtest.php">
What is your name?
<input type="test" name="name">
<input type="submit">
</form>
</body>
</html>
_END;
?>

PHP downloading instead of running [duplicate]

This question already has answers here:
Apache is downloading php files instead of displaying them
(27 answers)
Closed 1 year ago.
I am working on visual studio code making a few web pages and I need to send html form data from one to another. I am using the get method but when I submit the form the PHP file downloads instead of running, is there a way to fix this?
<form action="login.php" method="get">
<p class="normal" id="samelinechild">Dora the</p>
<input name="verification" id="very" type="text">
<p class="normal"id="samelinechild">explorer</p>
<input type="submit">
</form>
If you send data use POST method not GET. I don't know if this will fix the problem but it's the rule.

Ajax $.post not working in Firefox [duplicate]

This question already has answers here:
Send ajax request
(2 answers)
Closed 5 years ago.
I read a lot questions and answers about this problem, but noone solved mine.
So i have a simple form:
<form action="#" name="pm" id="pm" method="post">
<input onclick="sendPM()" name="pmSubmit" type="submit" value="Send" />
</form>
And the following function:
function sendPM() {
$.post("something.php").then(function(data) {
});
}
In "something.php" i doing a simple INSERT INTO to my database.
This code works perfectly in IE but dont work in Firefox.
I dont know, what can i do to solve this.
Pls if anyone can help me, will save a lot of time for me.
This sounds like magic, but fixed it. Change
onclick="sendPM()"
to
onclick="sendPM(); return false"
I don't know why, but now both Chrome and Firefox perform the ajax call. Previously firefox did not even make the connection to the server...

Issue with forms in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php $_POST array empty upon form submission
I am trying to have a form filled out, then the data displayed on another page. The first document is this
<html>
<head>
</head>
<body>
<form action="testerpage.php" method="post">
Name: <input type="text" name="testname">
<input type="submit">
</form>
</body>
</html>
The other page (testerpage.php) is this
<html>
<body>
<?php
echo $_POST["testname"];
?>
</body>
</html>
Why won't testerpage display the information from the first page(named welcome.php)? It doesn't work with "get" either
Given that you have untouched PHP code in your HTML output, it turns out PHP is disabled on your server so PHP code is not executed at all. Consult your hosting provider technical support on enabling PHP on server.

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.

Categories