8 Replies - 789 Views - Last Post: 11 August 2015 - 09:33 AM

#1 mre12345   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-August 15

How to add info into database via confirm popup?

Posted 09 August 2015 - 10:30 PM

Hello,

I have 2 pages, one is called addgames.php and another is addedgame.php. The addgames.php page is a form with a text input, once the user has inputted something and pressed submit, they will get redirected to addedgame.php. When the user gets redirected to addedgame.php, a confirm pop up box will appear where the user can either press Ok or Cancel. I want it so if the user presses Ok, the information from the text form in addgames.php will get uploaded to my database and if the user presses cancel, I don't want the text form to be uploaded. Right now, when I press Ok, 2 rows of data get uploaded to the database, one is blank with no values, and the other one is the information from the text form. If the user presses Cancel, only one row of data gets uploaded to the database which is the text form. I am really stuck and have been looking all over for an answer but I have not found one which is applicable to my problem. Here is the code for both files:

addgames.php:

<h1>Add new game</h1>
<form action="index.php?page=addedgame" method="post">
<input type="text" placeholder="Insert Category Here" maxlength="30" name="addcat"/>
<input type="submit" value="Submit">
</form>






addedgame.php:


<script type='text/javascript'>
var confirmCat = confirm('Are you sure you want to proceed?');
if (confirmCat == true) {

	
	<?PHP $accrosvar = $_POST['addcat']; ?>
	
	window.location='index.php?page=addedgame';
}

else {

	window.location='index.php?page=admin';
}
</script>








I have also attached a video of my problem.


Any help will be greatly appreciated!
Thanks,
mre12345

Is This A Good Question/Topic? 0
  • +

Replies To: How to add info into database via confirm popup?

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4192
  • View blog
  • Posts: 13,244
  • Joined: 08-June 10

Re: How to add info into database via confirm popup?

Posted 10 August 2015 - 12:52 AM

how do you save the data into the DB?

from the description I suspect that on submitting addgames.php you write data into the DB. on pressing ok, you call that insert script again (only without values, which you donít validate) and on pressing cancel no second insertion happens.

on the other hand side I wonder why you need an explicit confirmation page for something that already implies confirmation (if I press submit, I want to add the data, otherwise I donít press submit Ö)
Was This Post Helpful? 0
  • +
  • -

#3 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2020
  • View blog
  • Posts: 6,151
  • Joined: 15-January 14

Re: How to add info into database via confirm popup?

Posted 10 August 2015 - 09:46 AM

Note also that the line of PHP code in addedgame.php always gets executed regardless of which button they press. PHP and Javascript are not connected in any way. PHP runs first, on the server, and Javascript runs later, on the browser. That PHP code isn't sitting there waiting for some Javascript event to happen, it runs when the page is loading and then the browser runs the Javascript code. If you want to execute PHP code based on Javascript events then you need to use Javascript to send an ajax request to the server to run the PHP code.
Was This Post Helpful? 0
  • +
  • -

#4 mre12345   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-August 15

Re: How to add info into database via confirm popup?

Posted 10 August 2015 - 02:52 PM

Thanks this is the answer I was looking for. Is there any tutorial on sending an ajax request to the server running PHP code ?

EDIT: Sorry stupid me... the code from addedgame.php is not right. I accidentally copied and pasted the wrong document...


the real code for addedgame.php is:

<script type='text/javascript'>
var confirmCat = confirm('Are you sure you want to add <?PHP echo $_POST['addcat'];?>?');
if (confirmCat == true) {

	
	<?PHP 
	
	$add_cat = $_POST['addcat'];
$add_cat_sql = "INSERT INTO data (name) VALUES ('".mysqli_real_escape_string($link,$_POST['addcat'])."')";




	$add_cat_query = mysqli_query($link, $add_cat_sql);

	
	 ?>
	
	window.location='index.php?page=addedgame';
}

else {

	window.location='index.php?page=admin';
}
</script>



This post has been edited by Dormilich: 11 August 2015 - 12:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2020
  • View blog
  • Posts: 6,151
  • Joined: 15-January 14

Re: How to add info into database via confirm popup?

Posted 10 August 2015 - 03:30 PM

Ajax requests aren't PHP-specifc, any ajax tutorial should do. Ajax is just how you use Javascript to send a regular get or post request in the background, just like submitting a form or something except it's done only with Javascript. On the server, PHP would handle it like any other request. You can also send form data with the request that PHP would still be able to access via $_POST.
Was This Post Helpful? 1
  • +
  • -

#6 mre12345   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-August 15

Re: How to add info into database via confirm popup?

Posted 10 August 2015 - 07:10 PM

Iv'e been browsing the web for an ajax tutorial and they are all very confusing (as I don't know any javascript). Do you know any VERy basic videos/web tutorials that show how to send an ajax request to a server to run my PHP code?

Sorry for all the replies I'm just very new to all of this =\

This post has been edited by Dormilich: 11 August 2015 - 12:15 AM

Was This Post Helpful? 0
  • +
  • -

#7 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 801
  • View blog
  • Posts: 3,264
  • Joined: 13-June 14

Re: How to add info into database via confirm popup?

Posted 10 August 2015 - 09:58 PM

If you're finding it confusing because you haven't learnt any Javascript, I'd suggest at least learning the basics.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4192
  • View blog
  • Posts: 13,244
  • Joined: 08-June 10

Re: How to add info into database via confirm popup?

Posted 11 August 2015 - 12:18 AM

Quote

EDIT: Sorry stupid me... the code from addedgame.php is not right. I accidentally copied and pasted the wrong document...

that code confirms what ArtificialSoldier already mentioned, PHP executes separately from (and most of all before) JS.
Was This Post Helpful? 0
  • +
  • -

#9 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2020
  • View blog
  • Posts: 6,151
  • Joined: 15-January 14

Re: How to add info into database via confirm popup?

Posted 11 August 2015 - 09:33 AM

Start here and go down the list until you understand it:

https://www.google.c...=UTF-8&oe=UTF-8

Like ndc85430 said, you might have an easier job if you learn the basics of Javascript first.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1