Prompt not working with IF statement - php

Again, I'm very new to programming with PHP and JS. I pulled off a piece of code that is a JS prompt function within PHP to confirm a modification to data. The $confirmdelete variable is indeed "YES" when I type that in the prompt... (I checked it with an echo), but it keeps producing the "MODIFICATION ABORTED" message no matter what (and of course not changing the data).
Is my IF statement bad? Hope it is just a newb typo... Is this even a good way to do this? Thanks for any help...
<?php
//prompt function
function prompt($prompt_msg){
echo("<script type='text/javascript'> var answer = prompt('".$prompt_msg."'); </script>");
$answer = "<script type='text/javascript'> document.write(answer); </script>";
return($answer);
}
$prompt_msg = "Are you SURE you wish to make a modification? Type YES to confirm: ";
$confirmdelete = prompt($prompt_msg);
if ($confirmdelete != "YES") {
echo "MODIFICATION ABORTED <br><br>
<a href='index.php'>RETURN TO MAIN PAGE</a>";
exit();
}
?>

Ummmmm....
You cannot do that.
When the PHP code is outputted -- it is done. There is no more talking back to the server unless you add some AJAX handlers.
The only think your code outputs is this:
<script type='text/javascript'> var answer = prompt('Are you SURE you wish to make a modification? Type YES to confirm: '); </script>MODIFICATION ABORTED <br><br>
<a href='index.php'>RETURN TO MAIN PAGE</a>
Demo

Related

Issue with Calling JS function through PHP based on IF statement

I have a web based tool. There is a login where username, loggedin and authorised are stored in session variables.
There is one particular page where I have a form that has multiple buttons, I wish to disable one button based on what the users authorisation level is. So if authorised is 0 (user) the button is disabled, else it's enabled as I've only got two authorisation levels, 0 & 1.
I've attached what I've done below, and to me it looks right, obviously it's not!
Here is the JQuery function:
$(function disable(){
$('#signBtn').attr('disabled', true);
});
Here is the PHP code:
if($_SESSION['authorised'] == '0')
{
echo "<SCRIPT LANGUAGE='javascript'>disable();</SCRIPT>";
}
Here is my HTML code:
<input type=\"submit\" name=\"save\" id = \"signBtn\" class = 'eBtnSubmit' value=\"Sign off by Chairperson\" />
If there is anyone out there that can see what my problem is I would really appreciate it...this is my last piece of the puzzle and I'm presenting this today (Software Intern).
So basically if the level is 0 call the function.
Regards,
Gary
No warranty because I don't have all the code
Change the static JavaScript Code to this
var disableSingoff = function () {
$('#signBtn').attr('disabled', true);
}
In your original code, you execute the disable() function right when the DOM is ready by wrapping it in $().
Change the PHP code to this
if($_SESSION['authorised'] == '0') {
echo "<script>$(function () { disableSignoff(); })</script>";
}
It might be easier to simply set the value in the HTML if possible
if($_SESSION['authorised'] == '0'){
echo '<button type="button" disabled="disabled">Click Me!</button>';
}else{
echo '<button type="button">Click Me!</button>';
}
This would work before the page even starts loading.
The other option would be to hook the window on load function.
window.onload = (function(){ [...] }
is the <input type=\"submit\" name=\"save\" id = \"signBtn\" class = 'eBtnSubmit' value=\"Sign off by Chairperson\" /> being echoed from a php statement? if not you can get rid of all the '\'s. they are not necessary when writing strictly html.

Failng to display "data saved" message in the front end

I am converting all the values from xml to CSV successfully in the 2nd page(Export page). But i want to display a message "data converted successfully" in the first page(seat-matrix ).
I am failing to display the message in seat-matrix.php (1st page). Please help me.
In "Seatmatrix.php" file I have the following code.
<form name="export" action="export.php">
<input type="submit" name = "export" value="Export" title ='Exports all the above info to excel'>
</form>
In export.php file I have alert function and included header function to redirect to seat-matrix page as show below.
<?php
echo "
<html>
<head>
<SCRIPT LANGUAGE='javascript'>
function Result() {
alert (\"Data exported successfully\");
}
</SCRIPT>
</head>
<body>
";
// export feature code
echo "<SCRIPT LANGUAGE='javascript'>Result();</SCRIPT>\n";
header('location:Seat_matrix.php');
?>
Javascript doesn't get run until the browser gets it. Because of the header redirect it will never get run in the browser.
Your best bet is to set a session variable, then check if it is set in Seat_matrix.php. If it is, add your javascript.
A simpler way would be to post the result back to the original page and have it check. You would need to change your code to something like this:
if (isset($_GET['result']) {
if ($_GET['result'] == "success") {
echo "Data exported successfully.");
}
else {
echo "Error exporting data.";
}
}
<form name="export" action="export.php">
<input type="submit" name = "export" value="Export" title ='Exports all the above info to excel'>
</form>
And in the other code add the result variable to the url:
header('location:Seat_matrix.php?result=success');
Summary:
The first time you call Seat_matrix.php, there is no result variable and it program runs at it does now. Once the second program is executed, it calls back Seat_matrix.php and passess the variable result. The second time Seat_matrix.php is called, it checks if the result variable was passed and shows the message. You will probably have to play a little bit with the location where you want the message.
I hope this helps. Good luck!
Edit
To get rid of the $_GET variable after you write the success message, you could try something like:
if (isset($_GET['result']) {
if ($_GET['result'] == "success") {
echo "Data exported successfully.");
}
else {
echo "Error exporting data.";
}
unset ($_GET);
}
I haven't tested the above method, but I believe it might work.
You can use JavaScript to do the redirect instead:
<SCRIPT type='text/javascript'>
alert ('Data exported successfully');
window.location = 'Seat_matrix.php';
</SCRIPT>

passing value to another page using javascript

Hello every I want to pass a message from php to JavaScript function and then redirecting to another page i want to show that message . Plz help me out . here is the sample code
Php
if ($result){
$success = "New Page has been added successfully.";
} else {
$error = "Unable to process at this time.";
}
Java script
<script type="text/javascript">
window.location="admin.php?page=add_user";
</script>
i want this &success message on another page by using this javascript function . i have tried in this way
window.location="admin.php?page=add_user&<?php echo $success?> ";
check this
echo '<script type="text/javascript"> window.location="test.php?page=add_user&msg='. $msg .'"; </script>';
Because you have spaces in your response, in order for it to be useful in the url you need to url encode it. See php urlencode function. Also, you have to give name to the query string parameter.
Change you JavaScript like this:
window.location="admin.php?page=add_user&success=<?php echo urlencode($success); ?>";

I need POP up window after IF and ELSE statement

I need pop up window after IF and Else statement heres my code.
else if ($selected_radio=='q1yes' && $status=='yes' && $bus=='retail' && $retail=='retailyes' && $phonetype=='dsl' && $checks=='checkyes' ) {
$q1yes_status = 'checked';
$yes_status = 'checked';
$service_status='checked';
$autorepair_status='checked';
$analog_status='checked';
$checkyes_status='checked';
echo "your answer is yes yes retail autorepair dsl yes"; <<<< --- that text should change pop up window URL like "http://formsignup.php" instead of text message. I'm not sure if that possible to do Pop up inside ECHO.
I appreciated your help!
Would the POP up window be done is JavaScript?
So where you wanted a POP up window you could echo/print the required JavaScript?
Using code like...
window.open("http://formsignup.php","Window Name",width=430,height=360");
This code could be included in the HEAD of the document, and then called in the main BODY.
Or this could all be put in the BODY like...
<script language="JavaScript">
window.open("http://formsignup.php","Window Name",width=430,height=360");
</script>
echo some javascript code, then when the page sis sent to the browser the browser will fire the javascript code causing your popup to show:
echo "<script>alert('your answer is yes yes retail autorepair dsl yes');</script>";
for example:
You can echo some javascript that loads a new window?
echo "<script>window.open('url to open','window name');</script>";

improve my PHP user interaction

I have a situation where a user fills out 1 of 2 forms on a registration page and is sent to a software download page. If they sign up as a new user, form is processed inserted into a MySQL database and they go to the page no problem.
Here is my issue. If they are a returning user and enter a license key, the processor script checks to see if its valid against the database and if it is it sends them to the software download page. If it is NOT a valid license key (heres what I dont like) the screen goes to the url of the script, page is white, an alert pops down telling them its not a valid license key and they are returned to the registration page to try again. I hate this. I need to figure out a way to either pop the alert on the registration page w/o leaving it or better yet display some kind of message on the page. One drawback is that the script is and always will be on a different server than my forms. Ive tried curl and had success with other situations but can't close the MySQL connection on this one. Is there another way to achieve some semblance of "cross domain AJAX" I would really like it to not go to the script url/white page/alert then return them. I would like it to happen all on one page. Here is that part of my script:
if ($_POST['license_code'] != "")
{
$result = mysql_query("(//mysql stuff here)");
if (($row = mysql_fetch_assoc($result)))
{
header("Location: http://" . $redirect);
}
//here is the part I dont like
else
{
echo "<html>\n";
echo "<body>\n";
echo "<script language=\"Javascript\">\n";
echo "alert (\"The license ID you entered was not correct.\");\n";
echo "window.location=\"http://www.registrationpageURL.php\";\n";
echo "</script>\n";
echo "</html>\n";
echo "</body>\n";
}
mysql_close($link);
}
//I use jquery valiadate.js for CS validation, but realize this is necessary and would like it to behave like the desired result for the above
else
{
if (strpos($_POST['email1'], '#') === false)
{
echo "<html>\n";
echo "<body>\n";
echo "<script language=\"Javascript\">\n";
echo "alert (\"The email address you entered was not correct.\");\n";
echo "window.location=\"http://www.registrationpageURL.php\";\n";
echo "</script>\n";
echo "</html>\n";
echo "</body>\n";
return;
}
thx
Is it possible to remove the alert and when you redirect to registrationpage.php also send a parameter using the redirect url and popup an alert or error message after the redirect ?
Look into using AJAX. jQuery has a great API for this:
http://api.jquery.com/jQuery.get/
http://api.jquery.com/load/
EDITIED - For cross-domain
You could do something like this:
<div id="results"></div>
<script type="text/javascript">
$("#the_form").submit(function() {
$.getJSON("http://remote.domain/script/to/validate.php?data=" + escape($(this).serialize()) + "&callback=?", function(data) {
$("#results").html(data);
});
return false;
});
</script>
This will (once the IDs are pointed at the correct elements) intercept the form submission, pull together the values from the form (through the serialize() function), and shoot it out to the validation script via AJAX. The output of the script is displayed in the #results div.
Hope this helps!

Categories