Jump to content


confirm on JS n insert data on PHP


4 replies to this topic

#1 joe

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Location:stuck in the middle of cyber space

Posted 17 May 2007 - 02:46 AM

Hi guys... :biggrin:

I've some problem in inserting data, but before inserting data into table i want to use confirmation using JavaScript to ask user about inserting data..
this the script :

<?php
  //file name : [b]function .php[/b]
  //function to insert data into the table
  function insertData($sql)
  {
	//confirm the user
?>
	<script language=JavaScript>
	 <!--
	 if(confirm('Are you sure want to add new user ?') == true)
	 {
<?php
	   $query = mysql_query($sql) or die (mysql_error());
?>
	   alert('Input Data Success..!!');
	 }
	 else
	 {
		alert('Input Data Cancel by user..!!');
	 }		
	 //-->
	</script>
<?php
  }
?>

<?php
  [b]file name : insert.php[/b]
  include ('function.php');

  //i cut the source, cos i think the problem start in here... =)
  $sql = "INSERT INTO user(id_user,name,pass) VALUES('$increment','$v_name','$v_pass2')";
  insertData($sql);
?>

problem :
when browser display confirmation dialog contain and i choose cancel, it should be don't add anything in to the table, but fact is it add data into the table... :mellow: :wacko:

thanx 4 ur reply... :wink:

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 17 May 2007 - 12:43 PM

I'm not going to get to into this because somebody will probably end up dead (namely me). So just read this:
http://php.net/manual/en/faq.html.php#faq....script-variable

#3 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 17 May 2007 - 03:30 PM

Ya, I feel like being patient I guess...

PHP and JavaScript are completely different things! They cannot interact like that!

PHP is executed on the server, and the JavaScript is only executed by the user. So what you are doing is echoing part of a JS script, grabbing a MySQL resource (and doing nothing with it), then echoing the rest of the JS.

If you want to not have to reload the entire page every time, you need to use AJAX.

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 17 May 2007 - 04:41 PM

No, AJAX is not even needed for this.

All he needs to do is (assuming he is using a form) use the onSubmit even to return window.confirm("Are you sure you want to add the user?"); Then the form either will or will not submit, add the user, and display a "congradulations" message.

If he really doesn't want the page to refresh, he could do the exact same thing, but make sure it returns false every time.
i.e.
<form method="post" action="file.php" onSubmit="if(window.confirm("blah")){ ajaxFunction(); } return false;">

Edited by rc69, 17 May 2007 - 04:43 PM.


#5 joe

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Location:stuck in the middle of cyber space

Posted 18 May 2007 - 07:55 PM

Thanx guys...
I've found the answer and thanx 4 ur reply and ur suggest... :dance: :o

cheerrss...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users