Call a webpage in background in PHP and use jQuery on it - php

I have an HTML page which has a text box and a button...
<html>
<head></head>
<body>
<form name = "inputdata" method ="get" action ="prc.php">
<input type="text" name="data">
<button id ="checkbox" type="buttton" name="submit" value="submit">Submit!</button>
</form>
</body>
</html>
The code for prc.php is...
<?php
$data=$_GET['data'];
echo "<html><head></head><body><div id=\"maindata\"> ";
echo $data;
echo "</div></body></html>";
?>
I have a PHP file which gets the contents of the previous page...
<?php
file_get_contents("index.html");
?>
However, I would like to add functionality to the above PHP script such that it gets the page contents in the background and uses some jQuery on it:
$('#inputdata').val('hello boss');
$('#checkButton').click();
This will enable me to fill out the text box in the background and submit the button. Then I need to get the result from prc.php using jQuery...
$('#maindata').html();
Does anybody know how to do this?

It sounds like you want to submit the form on the page. This should be possible by simply requesting the resource in the action field. In your example, requesting the url prc.php with a parameter called data which contains the data input data you wish to submit.
prc.php?data=mydata

Related

Processing Form Data using GET Request in PHP

I'm new to php and I am writing code to get form data using get method. Following is my code in index.php file.
<!DOCTYPE html>
<html>
<body>
<form method="GET" action="index.php">
<p>Enter Name</p>
<input type="text" name="fname" />
<input type="submit" />
</form>
<?php
if($_SERVER['REQUEST_METHOD'] === 'GET')
{
$name = $_GET['fname'];
print $name;
}
?>
</body>
</html>
when I run my code, it calls the php code before submitting form. How can I process the data using GET Method without creating a new php file.
it calls the php code before submitting form
Because when you load a page, that's a GET request. And the code explicitly states to execute on a GET request.
How can I process the data using GET Method without creating a new php file
You'd need to determine the difference between when the page is loaded and when the form is submitted. If the form must use GET then the request method isn't that difference. One option could be to check for the existance of a submitted value. For example:
if (isset($_GET['fname'])) {
// your code
}
A common approach would be to use the name of the submit button being clicked as well, which can also be used to distinguish between different buttons in the same form. But any submitted value will do.

PHP how do display the inputted name of a user

I'm very new to PHP, if you could help me out that would be great :)
what i am trying to get is a way to display the inputted text with a echo en $_POST function. Ive read about it, but cant really get it to work. For more clarity here is an example. The person types his name in the box and click the "go" button. It would then display: Welcome (name of the person). Could this be done in one PHP file that would then redirect to index? What i mean is that a button is clicked in welcome.php you will then be redirected to index.php and so on.
Thanks is andvance
Variable:
<?php
$variable = 'hello world';
echo $variable;
?>
you need 2 php files
if you post something it's pretty much the same
You do need to have a html input element with 'name' attribute
php file that holds the html
<form method="POST" action="your_php_file.php">
<input type="text" name='name' value="some value">
<button type="submit">Go</button>
</form>
php file that holds the php code basically (your_php_file.php) the form from php file 1 post to this one
<?php
$variable = $_POST['name'];
echo $variable;
?>

How can my PHP code run when i submit a HTML form

I am trying to make a website using python and in flask I have a webpage that has a submit button, when the button is pressed I need it to run a python file I have called Answers.py
from flask import Flask, request, render_template, send_from_directory
app = Flask(__name__)
#app.route('/help', methods=["GET","POST"])
def help():
return render_template("help.html")
the HTML code on the page is this and i am trying to get when the button is pressed it runs a php script that outputs the value from a separate python file, the output text.
<html>
<body>
<form action="" method="post">
<button type="submit" name="sub" value="Hello world">Submit
call</button>
</form>
</body>
</html>
I have tried this php code in the same file as the HTML but nothing is output when I press the button, i'm new to php so i'm not sure what the problem is.
<?php
if(isset($_POST['sub']))
{
$result = exec("Python Path Answers.py /tmp");
echo $result;
}
?>
The php script never gets referenced in the html page!
<form action="" method="post"><!-- action there is empty! -->
You should reference the php script in the action attribute of the <form> tag, like so:
<form action="myscript.php" method="post">

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

PHP with HTML textarea

I am quite new to PHP/HTML all this kind of stuff so sorry if this is an easy question!
Is there a way to read the contents of an HTML text area and pass it into a .php file?
Or even just reading the contents of the text area into a variable would do.
Thanks
You can achieve that with a basic form:
index.php:
<?php
if ($_POST) // If form was submited...
{
$text = $_POST["mytextarea"]; // Get it into a variable
echo "<h1>$text</h1>"; // Print it!
}
?>
<form method="post">
<textarea name="mytextarea"></textarea>
<input type="submit" value="Go!" />
</form>
And this can be done whatever input element you want e.g: inputs type text, password, checkbox, radio or a select or even and fresh HTML5 input type.
Give your textarea a name and post it to a PHP script with a form. The data from the textarea will be available in the $_POST['textareaName'] variable.
HTML
<form action="page.php" method="post">
<textarea name="myTextarea"></textarea>
<input type="submit" value="go" />
</form>
PHP
<?php
echo $_POST['myTextarea'];
?>
You can Post the form and can access the variable of the form with $_POST['myObj'];
Looking at the $_POST and fetching the value from the post through object name.
Here, "myObj" is an example object name.
you need to submit the form to get the textarea value. Or you can use Jquery to get the textarea content

Categories