how to add a javascript alert with a PHP script? - php

let's say i have an input form, wherein, it's use for an email address. this form is part of a long form so i use an alert box instead when other inputs got errors....my question now is, if I checked the email input string via php, if it has been taken , how will I put the message like e.g "this email has been taken" in an alert box if i am using PHP to check it from backend ?..i want an alert box, since I use it with the other input boxes that don't need a backend check
e.g
alert("$errormessage");

PHP is server-side language. you can output it to user with
echo "<script>alert('".mysql_real_escape_string($errormessage)."');</script>";
or you can write your own function
function alert($a){
echo "<script>alert('".mysql_real_escape_string($a)."');</script>";
}
alert("test");

You can echo any kind of javascript with PHP. The question is however, are you sure you want to do this? You can also do like this:
<?php if(emailExists): ?>
<script>alert('Email exists!')</script>
<?php endif; ?>
But you could also use jQuery and Ajax requests to check if email exists with ajax when user has typed the email.

echo '<script>alert("ssss");</script>' ; just put it within echo it will work that way

Related

Running JQuery only if the page has POST data

I have a page that I would like a block of jQuery code to run ONLY if the page has already been submitted through a form and has POST data. Currently I have the item running using .onload but I want it to only do that in the situation someone has already hit submit. I am not sure what the best way to do this is.
Is there a simple condition check for this? The page, when submitted, calls itself if that makes any difference.
You can not check whether a page has POST data or not via JavaScript. You can do it by PHP and with that you can call JavaScript
<?php
if (!empty($_POST))
echo '<script type="text/javascript">
runJquery();
</script>';
?>
runJquery() is the JavaScript function which will do what you want if their is any POST variable in the page.
You can do this by adding this conditional to your PHP code:
if(isset($_POST['x'])){
...your code here..
}

Calling paper-toast with PHP

I am trying to show a paper-toast when the user forgets to fill in some data in a form or when he submits a wrong e-mailadres.
I have this PHP-code that will print out an error message on the screen when the user submits the form and forgets to fill in the input or when he submits a wrong e-mailadres. This works fine. Here's a small part of the code:
<?PHP
if(isset($errorMsg) && $errorMsg) {
echo "<p>*",htmlspecialchars($errorMsg),"</p>\n\n";
}
?>
I want to make it so, that the error message appears in a paper-toast. Further, i want to display a paper-toast when the form is successfully submitted.
My question is: is it possible to call a paper-toast with the error message and appears when the form is submitted?
Thank you in advance,
The only way PHP can "trigger client side events" is by outputting HTML that will behave as you want. In your case, you basically need to output the HTML for a toast and make sure it opens as soon as the page is loaded. To do that, just set the opened attribute:
printf('<paper-toast text="%s" opened></paper-toast>', htmlspecialchars($errorMsg));

Call jquery from php

I have a form with Name : input and a submit. When pressed, it posts to the same php file. My first check is basically if(!$name) { call jquery to insert error class }. I have the jquery set up in a function but I'm not sure how to call the function from the if statement.
You need to do your check in javascript / jquery and avoid posting to the php file until the javascript validation is completed / satisfactory.
Then in php you need to validate again in case the visitor has javascript disabled.
Don't use jquery in this case. Just have PHP output the appropriate class in the HTML, since PHP can not directly (or even indirectly) call javascript functions:
<?php
$name_error = empty($name); // $name_error is true/false;
?>
[...snip...]
<div class="this and that <?php if ($name_error) { echo 'error classname here'; } ?>">
<?php if ($name_error) { echo 'error message here'; } ?>
</div>
Trying to get PHP to call javascript to do what PHP can already do perfectly well on the server is a waste of effort. It's like driving to a payphone instead of using the perfectly good phone that's already sitting on your desk.
I think my answer for this question should prove helpful.
In a nutshell - your PHP script will need to send data back to the client that will let you identify which field is in error and why. jQuery will then be responsible for altering the field as you see fit.

Redirect to previous page and display error in a alert box

In my project, the first PHP page takes input from user and submits it to the second PHP page. In the second PHP page, the values which come from first page are validated against values from database, using functions. I want to redirect to the first page if any of the values submitted is wrong. Please help me out with the code. If not possible in PHP please mention the code in any other like jQuery or Ajax.
Here is code that does exactely what you want (according to your title):
<script type="text/javascript">alert("Stupid message");history.go(-1);</script>
I don't like this way of working, you'd better to use sessions, or creating one file that displays the form and does the validating.
#M LOHIT
<script type="text/javascript">alert("Stupid message");window.location.href='previouspage';
</script>
This will surely work for you
echo"<script type='text/javascript'>alert('Data Failed');window.location.href='index.php';</script>";
This might help you
echo '<script>alert("your error message"); location.replace(document.referrer);</script>';

What to use instead of if(isset($_POST['submit'])) for this.form.submit()?

I want to run a particular block of PHP if the user submits a form. It works if I use a submit button with name="submit" and:
<?php
if(isset($_POST['submit'])) {
code to run
}
?>
I don't know anything about javascript, and I want the code to run if the user changes a dropdown menu. If I make the first line of the dropdown
<select name="dropdownname" onchange="this.form.submit()">
the form appears (I haven't tested it) to submit if the user changes the dropdown choice. However, if I do this, the if(isset($_POST['submit'])) PHP code doesn't run. Is there a PHP if statement I can write that will respond to the form being submitted even though it's being submitted by a change in the dropdown and not a submit button?
You may want to check directly for:
if(isset($_POST['dropdownname']))
Even you can use something like:
if(count($_POST)){
// form validation
} else {
//...
}
you should always check $_SERVER['REQUEST_METHOD'] instead of particular field name
And in case if this dropdown used to display some data, not to write to the database, GET method should be used instead.
<?php
if(!empty($_POST))
{
code to run
}
?>

Categories