I have a web site that has all of the error messages displaying in a "div" at the top of the page. I need to change the code so that the errors display as alerts instead, but I can't figure out how to get the timing correct.
I've created a more simplified example of what I'm trying to do.
I want the following to happen:
- User types a letter into the "letter" input.
- User clicks a submit button.
- The form posts and the PHP code checks what letter was typed and sets a variable ($fruit).
- The HTML re-renders, and if the variable ($fruit) has a value, it is placed in the "fruit" input. (The purpose here is to put the result in a place Javascript can get to it.)
- The Javascript then executes and displays an alert showing the contents of the "fruit" input.
Here is the Javascript:
/* myTestScripts.js */
$(document).ready(function() {
window.onsubmit = function (e) {
e.preventDefault();
alert($("#fruit").val());
};
});
And here is my index.php file:
<!DOCTYPE html>
<head>
<!-- Style for Zebra Dialog boxes -->
<link rel="stylesheet" href="zebra/zebra_dialog.css" type="text/css">
</head>
<header>
<h1>Testing My Dialogs and Alerts</h1>
</header>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$user_input = trim($_POST["letter"]);
if ($user_input == 'A') {
$fruit = "apples";
} elseif ($user_input == 'B') {
$fruit = "bananas";
} else {
$fruit = "No fruit for you!";
}
}
?>
<form id="form_to_submit" action="index.php" method="POST">
<fieldset>
<label>Type A for apples and B for bananas:</label>
<input type="text" name="letter" id="letter">
<input type="text" name="fruit" id="fruit" value="<?php if(isset($fruit)) {echo $fruit;} ?>">
<button type="submit" id="click">Click Here to see your fruit selection.</button>
</fieldset>
</form>
<!-- Link to jQuery file so any plug-ins can work
Including the production version here. Can also download one for better debugging. -->
<script type="text/javascript" src="jquery-1.12.0.min.js"></script>
<!-- Link to My Javascript code -->
<script type="text/javascript" src="myTestScripts.js"></script>
</body>
Am I going about this the wrong way?
THREE DAYS LATER....
OK it seems like everyone is telling me to go with AJAX. So I've been watching tutorials and I get the general idea, but I'm confused about the URL I should pass.
Here is my updated Javascript: (It's a big wordy, but I'm trying to code this to get it clear in my head.)
/* myTestScripts.js */
function sendAjax() {
// Use ajax short-hand for 'get'
$.ajax({
type: "POST", // Post vs Get
url: "index.php", // url
context: $('#letter').val() // data
}).done(function() { // callback function
alert("Here is your fruit: " + $('#fruit').val()); // Show alert based on data here
});
}
$('#theButton').bind('click', sendAjax);
This only works party. It does not update the "fruit" input, so it doesn't display it in the alert either.
If my PHP code that processes this info is inside my "index.php" file, is THAT the URL I pass in? Or do I have to take all the processing code OUT of index.php, save it as a separate file, and pass THAT NEW file in?