I started learning php and I came accross this issue. I have the html code in my form:
<label for="u">Usability <span id='u_score'>0</span> / 7</label>
I want to get the inner html of the span with id u_score. How do I do this in php? Any help would be appreciated.
I have already tried
$u_score = $_POST["u_score"];
but that returns undefined because there is no value associated with the span.
Thank you
What you can do is to copy the span text content into a hidden input which will be posted:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function copySpanContent() {
document.getElementById("u_score_value").value =
document.getElementById("u_score").firstChild.data;
}
</script>
</head>
<body>
<form method="POST" action="PHPSCRIPT.php" onsubmit="copySpanContent()">
<label for="u">Usability <span id='u_score'>0</span> / 7</label>
<input type="hidden" name="u_score_value" id="u_score_value">
<button type="submit">submit</button>
</form>
</body>
</html>
Then you can get the value from PHP with $_POST['u_score_value'].
You can use Simple HTML DOM, it is an HTML parser for PHP. Download it then use this snippet:
include('simple_html_dom.php');
$html = file_get_html('yourHTMLfile.html');
$IDs = $html->find('span #u_score');
foreach($IDs as $id)
{
echo $id.'<br />';
}
Hope this helps!
php actually generates the html and does not perform any work (unless called via a method like ajax) to affect the page after it has completed building.
Javascript can get DOM element values, but PHP can not.
the method you are using will not work as the variable was not a $_POST variable but just an HTML element.
Please learn the dividing line between client (Javascript) and server (PHP).
You could use PHP to deliver the HTML that you require.
You could also use Javascript to look at the DOM via getElementById to do the business.
Related
This question already has answers here:
jQuery autocomplete with callback ajax json
(6 answers)
Closed 1 year ago.
I'm new to PHP and trying to implement an autocomplete as a proof of concept for a project for work.
The following is the code for the web page.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- jQuery UI library -->
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<!-- Initialize autocomplete -->
<script>
$(function() {
$("#skill_input").autocomplete({source: "search.php",});
});
</script>
</head>
<body>
<div class="container">
<h4>Auto complete Input for countries</h4>
<form method="post" action="submit.php">
<label > Your Skills:</label>
<input type="text" id = "skill_input" name="skill_input" placeholder="Start typing..."/>
<input type="submit" name="submit" value="SUBMIT">
</form>
</div>
</body>
</html>
The page displays as it should. As I type a valid search term for what is in the DB nothing happens. e.g. "Ger"
I'm running the site on xampp.
My DB is loaded with countries.
I put an echo in my DB module to see if it is reached. It is not displaying unless I use the URL of the DB module directly. When I go to the DB module directly using the URL http://localhost:8012/Managers/search.php?term=Ger
I receive the following
we are in the search php file
[“Algeria”,”Germany”,”Niger”,”Nigeria”]
This seems to indicate that the DB module is working e.g. accepting a search value, accessing the DB and returning data as expected.
So it appears to me that the script
$(function() {
$("#skill_input").autocomplete({
source: "search.php",
});enter code here
});
</script>
is not sending any data to the "search.php" page.
My question is why? Can anyone help me understand why the page is not sending any data to search.php? I would assume that it is not even calling search.php as I'm not seeing the echo message unless I use the URL directly.
The return of data is resolved.
/* Toss back results as json encoded array. */
$json_array = array();
$json_array = json_encode($return_arr);
echo json_encode($return_arr);
The above code coverts the response from the DB to json and then returns the data.
So I'm just dabbling in this and most likely doing it wrong. But it's a proof of concept so I can get the powers that be to get someone to do it right.
I am using a php script for login and for posting variables from forms.
The problem: I am echocing out an html code that includes the css, html, and jquery scripts all in one file. Which is fine for the css and html portion, but the jquery stuff doesn't work.
Is there a way to place the jquery portion in my php script to make it function, or does it need to be split out and referenced/loaded in the html. (I believe that is the correct way, but I don't know how or where to start.)
code: (after binding to ldap and authenticating, this block is triggered)
// verify binding
if ($ldap_bind) {
echo "
<head>
<link - stylesheet \link>
</head>
<form class=\"form-horizontal\" action\"form.php\" method=\"POST\" enctype=\"multipart/form-data\">
<fieldset>
<body>
html code....lots of div boxes
<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script>
<script>
$(document).ready(function(){
Various Jquery functions that hide/unhide div boxes
});
</script>
</body>
</fieldset>
</form>
";
} else {
echo "<p class='text-error'>Unable to log you in:</p>";
First, your HTML structure is quite mixed up. Have a look at how I "reorganized" it below and feel free to read tutorials like this one about it.
Second, you can use the PHP tags (<?php and ?>) to avoid the multi-line echo and all the quote escaping mess. What is outside these tags won't be processed by PHP and sent as-is to the browser. So you can write "usual" HTML there.
Third, having your scripts outside HTML like <form> is a good practice. And the library calls commonly are placed in the <head>... But it's also common to have it near the end of the document, just above </html>, instead of the <head>. For sure, not oddly everywhere in the markup.
// verify binding
if ($ldap_bind) {
?>
<head>
<title>My page title</title>
<link rel="stylesheet" href="...">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<form class="form-horizontal" action="form.php" method="POST" enctype="multipart/form-data">
<fieldset>
html code....lots of div boxes
</fieldset>
</form>
<script>
$(document).ready(function(){
//Various jQuery functions that hide/unhide div boxes
});
</script>
</body>
<?php
} else {
?>
<p class='text-error'>Unable to log you in:</p>
Recently installed wkhtmltopdf. Was trying to capture the entire page in its current state, however, the below method seems to navigate to the initial state of that page without all the input fields that the user has entered.
PHP
shell_exec('wkhtmltopdf http://localhost/www/bolt/invoice.php invoice.pdf');
I was wondering if someone knew of an implementation of wkhtmltopdf that captures the current state of the page including any text entered in the text fields??
I appreciate any suggestions.
Many thanks in advance!
wkhtmltopdf hits the page independently of your current browsing session. If you hit it like that, you're going to get what anyone would see when they first go to your page. Probably what you want to do is save the current page using an output buffer, and then run wkhtmltopdf on the saved page. Here's some sample code:
sub.php
<?php
$documentTemplate = file_get_contents ("template.html");
foreach ($_POST as $key => $postVar)
{
$documentTemplate =
preg_replace ("/name=\"$key\"/", "value=\"$postVar\"", $documentTemplate);
}
file_put_contents ("out.html", $documentTemplate);
shell_exec ("wkhtmltopdf out.html test.pdf");
?>
template.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<h1>This is a page</h1>
<form action="sub.php" method="post" accept-charset="utf-8">
My Test Field:
<input type="text" name="test_field" value="">
<input type="submit" value = "submit">
</form>
</body>
</html>
Probably in the long run you should have some kind of base template that both pages would use, and one have some markers like value='%valueOfThisVariable%' in your input fields that you can replace with blanks when you present the fields to the user, and fill with the user data when you create the page that you want to write to pdf. Right now it's just going through and replacing all the name='this_name' with value='this_name->value'.
This is my html form and below this i included my php text.
But I am not getting correct output,i don't no where the problem is ?
I also included the output ,please suggest me what shuld i do?
<html>
<head>
<title>
Entering data into text
</title>
</head>
<body>
<h1>
Entering data into text
</h1>
<form action="text.php" method="post">
What is ur name ?
<input type="text" name="data" />
<input type="submit" value="send" />
</form>
</body>
</html>
This is my php text:
<html>
<head>
<title>
Reading data from textfields
</title>
</head>
<body>
<h1>
reading data from text field
</h1>
Thanks for answering,
<?php echo $_POST["data"];?>
</body>
</html>
Output:
reading data from text field
Thanks for answering,
problem is that ,data send is not included after response of sever
please help me as fast as possible
I can only speak for my own experience, but this works on my server. I'd suggest, then, that one of the following is true:
Your server isn't set up to handle php (though this would surprise me), also, as #gAMBOOKa noted (in the comments), if your server's not set up to handle php the script wouldn't output anything other than the raw text-contents of the php script, literally "<?php echo $_POST["data"];?>".
You're trying to access the pages through the filesystem (file:///path/to/file.html), rather than through your server (http://localhost/file.html).
If '2' is correct, move your web-page and php script into a directory within your server's document root, on *nix this could be something like /var/www/directoryName/ and access the page via http://localhost/directoryName/dataEntryForm.html. If you're on Windows, with IIS it could be C:\inetPub\directoryName\ accessed, as above, with http://localhost/directoryName/dataEntryForm.html.
Incidentally, please forgive me for not linking to a demo of the page running, it's just that I'd prefer not to run the risk of exposing my server to, presumably, vulnerable scripts.
Your full code is like this and I tested it, working perfectly.
<html>
<head>
<title>
Entering data into text
</title>
</head>
<body>
<h1>
Entering data into text
</h1>
<form action="text.php" method="post">
What is ur name ?
<input type="text" name="data" />
<input type="submit" value="send" name="btn_save" />
</form>
</body>
</html>
text.php
<?php
if(isset($_POST['btn_save']))
{
$data=$_POST['data'];
}
?>
<html>
<head>
<title>
Reading data from textfields
</title>
</head>
<body>
<h1>
reading data from text field
</h1>
Thanks for answering,
<?php echo $_POST["data"];?>
</body>
</html>
working perfectly.
I made an html file called test.html then I navigated to it as "http://site.com/test.html?test1=a" but the textbox stayed blank. Why is this?
Super simple code
<html>
<head>
<title>Test</title>
</head>
<body >
<input type=text name="test1">
</body>
</html>
The file should be a PHP file, so test.php.
Then maybe something like this:
<html>
<head>
<title>Test</title>
</head>
<body>
<input type="text" name="test1" value="<?php echo htmlspecialchars($_GET['test1'], ENT_QUOTES); ?>">
</body>
</html>
The reason it stays blank in your example is because there is no PHP code to put the value into the field. It isn't automatic. Also, on most servers (but not always), a file with an .html extension will not be parsed by PHP.
Also, passing it to the htmlspecialchars function will help prevent cross-site scripting.
HTML is just another file extension to a webserver, it's not going to do any kind of processing unless you've done something to make that so. Would you expect to open http://site.com/foo.txt?contents=helloworld and see "helloworld" in the browser?
I suggest you google up some tutorials (w3schools is usually good for this sort of thing) on PHP, then on "query strings" and how server side scripting works. You should be up and running with basic site scripting pretty fast.
It might be possible to read the URL via javascript and populate the textbox that way if you must use static html.
<html>
<head>
<title>Test</title>
</head>
<body >
<input type=text name="test1" value="<?php echo htmlspecialchars($_GET['test1']);?>">
</body>
</html>