call php page under Javascript function - php

Is it Possible to call php page under Javascript function?
I have an javascript function and I want to call php page if someone press okk
Here is my code so far
function show_confirm()
{
var r=confirm("Do You Really want to Refund money! Press ok to Continue ");
if (r==true)
{
alert("You pressed OK!");
return true;
}
else
{
alert("You pressed Cancel!");
}
}
and this Js function i m using here
<td align="center"><input name="Refund" onclick="show_confirm()" type="submit" value="Refund" /></td>
now i want if user press okk then call other php page ...

if you want to redirect to a page:yourphppage.php if the user pressed ok.
function show_confirm()
{
var r=confirm("Do You Really want to Refund money! Press ok to Continue ");
if (r==true)
{
window.location="yourphppage.php";
return true;
}
else
{
alert("You pressed Cancel!");
}
}

If you are looking for page direction:
window.location="index.php";
There are plenty other methods to redirect: http://grizzlyweb.com/webmaster/javascripts/redirection.asp

Place this where you want your redirect to happen:
location.href = "newpage.php";

Checkout
http://www.w3schools.com/xml/xml_http.asp
Then have a look at
http://mootools.net/docs/core/Request/Request
Any of the JS libraries prototype Jquery etc. have a wrapper to make requests and your life easier.
Then just have a button and set its onClick to be the request call to the server and listen for the response.
Some tips you can append data to the request URL to pass your PHP script variables.
Checkout
http://www.w3schools.com/tags/ref_urlencode.asp

If you only want to redirect the page to the "refund" page without using AJAX you can do something along the lines of the following:
(assumes $transaction_id variable is the id number you are looking to refund)
HTML Lines:
<td align="center">
<input name="Refund" onclick="show_confirm(<?php echo $transaction_id ?>)" type="submit" value="Refund" />
</td>
Javascript lines:
function show_confirm(transaction_id)
{
var r = confirm("Do You Really want to Refund money! Press ok to Continue ");
if (r == true)
{
alert("You pressed OK!");
location.href = "refund.php?transaction_id="+transaction_id;
return true;
}
else
{
alert("You pressed Cancel!");
}
}

Related

How to make my simple alert window work

i'm currentrly coding in php and when i click a button the message should me Would you like to delete this entry?
This is where I'm currently at
<button onClick="alert('Sure to delete entry?')">Remove Entry</button>
And sure it works. But when I press "x" to close the alert window it still deletes the entry.
How can you make a popup window that has the "Cancel" opition?
I would really appreciate the help! :)
if (confirm("Your question")) {
// do things if OK
}
Use confirm
Click here for examples
You can do this using the following JavaScript code:
if (confirm('Sure to delete entry?') == true) {
//write your code here to delete
}
The HTML
<a href="delete.php?delete=<?php echo $row['guestbook_id'];?>"
onclick="return areYouSure()">Remove Entry</a>
And the JS
function areYouSure()
{
var con = window.confirm("Are You Sure?");
if(con)
{
return true;
}
else
{
return false;
}
}

Javascript confirm & while looped echo

I have a small problem, I made a delete button with a PHP while loop which looks like this:
while($something = mysql_fetch_array($sql_something)){
$id = $something['id']
echo '<button onclick="delconfirm()">Delete</button>
}
this echo's a few delete buttons for some content. However I need user confirmation for deleting first, this is where onclick="delconfirm()" comes in.
my confirm looks like this:
function delconfirm()
{
var r=confirm("Are you sure you want to delete this content?");
if (r==true){
// ...do nothing i guess? it needs to redirect using the PHP echo'd link...
}
else{
window.location = "edit.php";
}
}
However, whether you press cancel or ok, it'll delete it anyway. How can I fix this?
Change it to this:
while($something = mysql_fetch_array($sql_something)){
$id = $something['id']
echo '<button onclick="return delconfirm();">Delete</button>
}
And then your function:
function delconfirm()
{
return confirm("Are you sure you want to delete this content?");
}
EDIT: If you want a more unobtrusive solution:
while($something = mysql_fetch_array($sql_something)){
$id = $something['id']
echo '<input type="button" value="Delete" data-id="$id" />';
}
And then some javascript to bind the event:
function bindButtons() {
var buttons = document.getElementsByTagName("input");
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].type == "button") {
buttons[i].onclick = function () {
location.href='somewhere.php?id=' + this.getAttribute("data-id");
}
}
}
}
and bind it to the window.onload, as per Ian suggestion:
window.onload = bindButtons;
Note: If you were using jQuery this solution would be easier and more elegant.
Working jsFiddle
If the user presses cancel then you need to stop the event from doing what it would normally do. Try this, for example:
function delconfirm(e) {
e = e || window.event;
if (!confirm("Are you sure you want to delete this content?")) {
e.preventDefault();
// This will prevent the event from bubbling up to the <a>.
e.stopPropagation();
return false; // For the ancient/crappy browsers still out there.
}
return true;
}
You need to stop/delete the current click event. After your code is executed the event sinks to the anchor and triggers a click. With MooTools just add 'new Event().stop();'. I think jQuery has also something like this.
EDIT: Hanlet EscaƱo is right. You can return true (the browser will redirect to the URL in the href, or false to let the browser do nothing)
In order to prevent to the HTML link to work, you have to return false in your js function or event.preventDefault() where event is an argument which is passed to the click event function
I did thin when putting a click event on the a element and not on an element inside the a tag. But it might work.

Submit button not working in IE/Safari, works in Chrome but form info is not getting passed

I have a form setup but for some reason the JS to submit the form works in Chrome but not IE9 or Safari. Interestingly enough in Chrome where the submit button does work none of the information gets passed.
Here is my submit button -
<img type="submit" src="lib/send_feedback.jpg" border="0" class="feedback-submit-img" />
Here is what JS it calls
// Submit form to next page
function submitForm() {
// document.forms["feedbackform"].submit();
document.feedbackform.submit();
}
// Submit form and validate email using RFC 2822 standard
function validateEmail(email) {
// Modified version original from: http://stackoverflow.com/a/46181/11236
var re = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
// Return true if email field is left unchanged
function originalText(email){
var defaultMsg;
defaultMsg = "Enter your email address (optional)";
if(defaultMsg == email){
return true;
}
return false;
}
// Verify or decline with error message
function validate(){
$("#result").text("");
var email = $("#email").val();
if ((validateEmail(email)) || originalText(email)) {
submitForm();
} else {
$("#result").text(email + " is not a valid email.");
$("#result").css("color", "red");
}
return false;
}
$("form").bind("submit", validate);
For the second part of my issue which may or may not be related to the JS issue is -
echo $POST['satisfaction'];
echo $POST['user_email'];
echo $POST['comments'];
if(isset($POST['user_email'])){
echo 'true';
} else {
echo 'false';
}
If you would like a better look at the page I am editing here is a link to jsfiddle
edit
As per Marco's request I removed the link from around the submit button and placed the onClick event onto the actual button itself. This absolutely fixed the issue on both IE and Safari. Now my remaining question/concern is why the POST data is not passing correctly to the next page.
Here is the complete source with George requested. - index.php
Page source gets passed to - feedback-accept.php
Also with that being said/posted, what is StackOverflow's preferred site to post source to?
In response to Brian's comment, if I cannot use failed without potentially breaking the POST data, what would be a good alternative/work-around?
try this code:
jQuery:
$(document).ready(function()
{
$('.validate').bind('submit click',function()
{
//Validate!
//is email
if( /..../.test( $(this).find('input[name=email]').val() ) )
{
alert('Error!!!');
return false; //STOP PROPAGATION
}
//Its okay!
//send form!
$(this).parents('form').submit();
//click event stop propagation
return false;
});
});
HTML:
Submit
OR
<input type="submit" value="Submit" class="validate" />
OR
<button class="validate" >Submit</button>
In a quick look, I would say you are having a conflict of functions here. When you design a Submit Type, you are telling the browser that that will submit your form (on its action), on the other hand, that is inside a link that is calling a JS function. If you're manufacturing the submit in your JS, try to consider not use a Submit Type and see if it works more properly.
You might also try to replace the link for a simple "onSubmit" inside the form's Tag and call the JS function from there. This way, when the browser identifies that the user hit the submit button, he'll call a JS Function as you wish and if this function returns true, the submit will be allowed by the browser.

How would I check the value in form before sending it to server using ajax

I want to check the value enter in the form by user. i have applied validation and its working. The problem is that if user enter any form value incorrectly and then clicks submit, the whole page is refreshed and all input data is lost.
I want that validations is checked before passing it to server. One of my friends told me its possible with AJAX. Can anyone guide a beginner on how to do this?
You can use javascript instead and save the server from transferring some extra KBs and calculations by using Ajax (which technically is javascript but you send the request back to the server)
Jquery has a plugin called validation that will make your life easier though:
http://docs.jquery.com/Plugins/validation
There is a live demo in the link above
For example if you wanted to validate the username you could do this
<script>
$(document).ready(function(){
$("#commentForm").validate();
});
</script>
<form id="commentForm">
<input id="uname" name="name" class="required" />
</form>
yes you can use ajax or otherwise with your current approach you can use sessions to store user data and prevent it from being lost. with ajax you can show response from the server to show to the user.
$.ajax({
url: 'ajax_login.php',
type:'post'.
data:(/*data from form, like,*/ id: $('#username').val())
success: function( data ) {
if(data == 1) {
$('.feedback').html('data has been saved successfully');
//redirect to another page
}
else {
$('.feedback').html('data could not be saved');
$('.errors').html(data);
}
}
});
ajax_login.php would be something like
<?php
if(isset($_POST)) {
//do form validation if it is valid
if(form is valid) {
saveData();
echo 1;
}
else {
echo $errors;
}
}
?>
Do not need ajax.
Just set the onsubmit attribute of your form to "return checkfun();" and define checkfun some way like this:
function checkfun()
{
if ( all things were checked and no problem to submit)
return true;
else
{
alert('ERROR!');
return false;
}
}

how to trigger a java script confirm box after the page loads completely

I need to reload a PHP script on my page when I submit a form. When I submit, it has to load on the same page. When the form is submitted, I need to show the confirmation window to the user. However, the problem is that the confirm box is shown while the page is loading (and the page doesn't finish loading until the user selects an option).
------------<? if(){?>
<script>
if(confirm){
}else{
}
</script>
I need the confirm box to be triggered after the page loads completely. Any ideas as to why this might be happening?
Add it to the window.onload event:
window.onload = function () {
if (confirm...) {
...then...
} else {
...else...
}
}
or (what is better) use jquery:
$(function () {
if (confirm...) {
...then...
} else {
...else...
}
});

Categories