5 Replies - 429 Views - Last Post: 03 January 2013 - 06:32 AM Rate Topic: -----

#1 ersaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 31-May 12

Problem with php or Jscript ?

Posted 28 December 2012 - 08:47 AM

Hello i'm trying to realize where i'm doing wrong if any one of u can tell me where i'm wrong i'll be thankfull .

so my problem is i have a button and when i click on that it's working OK but the problem if i reload the page the function excetuciton itself so ...

<script type="text/Javascript" >

function checkout()
  {
 		<?php
			include "database.php";
			$UserID = 0 ;
			$$Types = 1 ;

				if (!$mysqli->query("CALL h_check_out('$id','$ReservationID' , '$Client' , '$RoomNumber' , '$UserID', '$Types')")) 
					{
						echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
					}
			
		?>
		alert ("Check in realizaed succesfully");
		window.location.reload();
  }
  </script>


<input type="button" id="php_code" onclick="checkout()" value="Checkout"> 




So all is working ok but i want to executee the function just onclick of button not On Refresh of page .... ?
any suggestion or any help ?


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with php or Jscript ?

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3481
  • View blog
  • Posts: 10,038
  • Joined: 08-June 10

Re: Problem with php or Jscript ?

Posted 28 December 2012 - 08:54 AM

you are aware the the PHP part is always executed before any page load and that the JS function doesn’t do anything besides reloading the page?
Was This Post Helpful? 1
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: Problem with php or Jscript ?

Posted 28 December 2012 - 10:09 AM

I'm guessing he isn't.

Just to clarify the point Dormilich is making. PHP is executed server-side, on the server computer, before the page is event sent to the browser. Javascript is executed client-side, in the user's browser, long after all PHP code has been executed.

Putting PHP code into a Javascript function does not meant that the PHP code will execute when the Javascript function is called. The PHP code will always execute immediately, before the page is shown.

If you want to have Javascript call a PHP script, you must send a request to the server for it to execute the PHP script, either by redirecting the browser or sending a AJAX request. (Or, more recently, using WebSockets, but I don't suggest that until you are more comfortable with Javascript.)
Was This Post Helpful? 1
  • +
  • -

#4 ersaan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 31-May 12

Re: Problem with php or Jscript ?

Posted 03 January 2013 - 03:09 AM

Thnx Dormilich & Atli for ur answers , if u can help me or suggest how to make this in ajax calling the result of store procedures via ajax .
I fix it using page redirectind getting the procedure result. but now i want it to get the result using AJAX ... so i would appreciate if u have time to help or suggest somthing for me ...
thnx in advance ... : )
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3197
  • View blog
  • Posts: 10,721
  • Joined: 12-December 12

Re: Problem with php or Jscript ?

Posted 03 January 2013 - 06:31 AM

You could start with Ajax here, but I'm sure there will be good articles or tutorials on this site as well.
Was This Post Helpful? 0
  • +
  • -

#6 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: Problem with php or Jscript ?

Posted 03 January 2013 - 06:32 AM

View Postersaan, on 03 January 2013 - 11:09 AM, said:

... if u can help me or suggest how to make this in ajax calling the result of store procedures via ajax .


AJAX is not that difficult, if you know JS, but if you are not so good in JS i would recommend to learn some JS basics. After that I suggest, you start with a tutorial like this: http://www.tizag.com...orial/index.php to understand WHAT Ajax is and HOW it works. After you understahd what ajax does, it is easy to convert your script.

Ooh andrewsw was just faster than me ;)

This post has been edited by Anthonidas: 03 January 2013 - 06:35 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1