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.
Related
This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 3 months ago.
I'm trying to make a simple search engine (currently using 10 random html files I've made) but I can't get the php echo to tell me I search results (which I'm using to verify that the php file knows what the search query is.) I tried using just php
This V
<?php echo $_GET["query"]; ?>
I tried PHP in HTML
<html>
<body>
<p> Search: </p> <?php echo $_GET["query"]; ?><br>
</body>
</html>
But none if it worked. Here's the code for the HTML file.
<html>
<head>
<title>Search Test</title>
</head>
<body>
<form action="Searching.php" method="post">
Search: <input type="text" name="query">
<input type="submit">
</form>
</body>
</html>
When you run the search engine and type your query the php files shows up just showing the contents of the php file. (If its an html file with php in it its just "Search:" so the query isn't showing.) I expect it to just show what my search query was.
Since you’re sending the form over POST, you would need to use $_POST instead of $_GET.
Also, your current implementation is vulnerable to cross-site scripting. You should use htmlspecialchars when you echo content you cannot trust.
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>
I have found an issue with my site and I have isolated it to Jquery Mobile components. I am simply clicking a href link that calls another PHP page. On the second page I have set up a form and a Jquery form validation that checks to see if the SKU button has been selected and if so it should return an alert and cancel the form submission. The problem is that when I link to the second page from the first page the form validation code is not working.
If I refresh the page it works just fine. I isolated out the link to Jquery Mobile and the page works just fine. There is something that is being passed by Jquery Mobile that is keeping the page from loading in its entirety. Or, more likely there is some carry-over from the first page that disables the validation code on the second page.
I can add that if I get rid of the links to Jquery Mobile entirely on the second page (no styling at all) and I link to the second page from the first page, The Jquery Mobile formatting still carries to the second page. I have investigated this extensively and I can't find any previous mention of this problem. I have set up test pages to sort this out.
Page 1:
<html lang="en">
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1" >
<title>Customer Maintenance</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"> </script>
</head>
<body>
Test 1 page
<p>Home Page</p><hr>
<footer>Created by: attentionjay</footer>
</body>
</html>
<a href="test2.php" >Test Inventory</a>
Page 2:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1" >
<title>Customer Maintenance</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
<script>
$(function () {
$('#custalert').bind('submit', function (e) {
if($("input:first").val() == "") {
alert("You must enter a Customer Alert");
return false;
}
});
});
$(function () {
$('#queryinv').bind('submit', function (e) {
if($('#sku_checkbox').is(':checked') == true) {
alert("You must enter a SKU");
return false;
}
});
});
</script>
</head>
<body>
<div data-theme='a' >
<div style="padding:10px 20px;">
<form action="inventory_inquiry.php" method="get" id="queryinv" >
<h3>Enter SKU</h3>
<fieldset data-role="controlgroup" data-type="horizontal" data-mini="true">
<legend>Choose Input Type:</legend>
<input type="radio" data-theme="a" name="input" id="sku_checkbox" />
<label for="sku_checkbox">SKU</label>
<input type="radio" data-theme="a" name="input" id="entire_checkbox" />
<label for="entire_checkbox">Entire Inventory</label>
</fieldset>
<label for="sku" class="ui-hidden-accessible">SKU:</label>
<input type="text" name="sku" id="sku" value="" placeholder="Item SKU" data-theme="a">
<input name="customer_id" type="hidden" value='<?php echo $customer_id; ?>'/>
<button type="submit" data-theme="b">Submit</button>
</form>
</div>
</div>
<br>
<p>Home Page</p><hr>
<footer>Created by: attentionjay</footer>
</body>
</html>
Any help I could get would be great. This is a mystery to me.
EDIT: 7-1-2013
I thought that it would be worth updating what I found while solving this problem. Disabling the AJAX data works, but only for links. I had difficulty getting it to work on a form submission. I ended up disabling AJAX globally with the following script:
<script>$(document).bind("mobileinit", function(){
$.mobile.ajaxEnabled = false;
});</script>
This script must be placed before the Jquery Mobile script on the page. This will disable the AJAX functionality for the entire page. AJAX is great and it should be used when possible. I had already set up the layout of my site before I discovered my problem. A site could more easily be retrofitted to make use of the Jquery Mobile functionality.
Try adding data-ajax="false" to your <a> tag whenever you are linking to a seperate php or html page.
jQuery mobile is designed for developers to put multiple pages int he same html or php file. So it uses ajax to link between those 'pages' which are really just divs. The issue with this is that, by default, jQuery mobile uses ajax for every link unless you state otherwise. This can create some cooky problems and it took me a while to understand when I first started with jQuery mobile. I think there's a pretty good doc about this topic in the JQM documentation.
adding
rel = "external"
works for me
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.
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.