Page 1 of 1

How to run different scripts on different button submits?

Posted: Sun Sep 29, 2013 12:16 pm
by Fawkes
Hi,

I have 3 buttons on my form, two which are submits (Confirm and Modify) and one which is just a button for cancel (and a subsequent onClick script when it is clicked).

Currently, there is a piece of code that does the following:

Code: Select all

	echo "<form name='achievement_modify' method='post' onSubmit='return check_num();' action='".$_SERVER['PHP_SELF']."'>";
This causes a script to run (pretty much error checking) to ensure that there are checkbox fields selected if the user clicks Confirm to submit the form. However there is no need for the Modify button to run these same checks, so I am looking at ways to avoid the problem I have right now.

One solution I suppose I came up with, but I am not sure if it works, is that I do the modify button similarly to the cancel button and have it run its own onClick script but I am not sure how to write that script such that I can retain all the data like how the current "action='".$_SERVER['PHP_SELF']."" does it in the post array.

A second solution which I am trying to determine if possible is to make the check_num script check if it knows which button was submitted, so it can just return true if it is the modify one, else do the checks if it is confirm one. However again, I am not sure how to do this.

Anyone have any idea on how I can do this? I am not the original author of this code, I just volunteered to help with something for some acquaintances, that's why I seem to be asking questions about things that I should be able to solve if I wrote this code.

Thanks.

Re: How to run different scripts on different button submits

Posted: Sun Sep 29, 2013 12:56 pm
by Celauran
jQuery listener on the various buttons?

Re: How to run different scripts on different button submits

Posted: Sun Sep 29, 2013 2:18 pm
by Fawkes
How would I go about doing that? I've been trying various things but I am not sure how to get it to work...
Various lines of code relevant to the buttons would appear in first.php

Code: Select all

<input type='submit' name='mconfirm' value='  Confirm   '>

<input type='submit' name='modify' value='  Modify   '>
and my check_sum script is in first.js

I have like a next to not even beginner's knowledge on how php works and I am trying this. The interaction between html, php and js I have next to no clue about. I just know that there are calls to js scripts throughout the php code sometimes and there is "embedded" html code being echo'ed in the php files that pretty much create the page. How would I go about inserting code that would affect this modify button and also including JQuery from a CDN?

Re: How to run different scripts on different button submits

Posted: Tue Oct 08, 2013 12:33 am
by Fawkes
So far what I got was something along the lines of just loading the JQuery CDN...which was just copy and pasting some stuff.

Reiterating the problem: I have two submit buttons (confirm and modify) which both run a script because of this:

Code: Select all

"<form name='achievement_modify' method='post' onSubmit='return check_num();' action='".$_SERVER['PHP_SELF']."'>";
I want to implement something such that if modify was the button pressed, it would not perform the script by doing something like the following and then loading itself using the action:

new check_sum() would look like this

Code: Select all

if (modify was pressed) { do nothing } else do "old check_sum code

Re: How to run different scripts on different button submits

Posted: Tue Oct 08, 2013 8:40 am
by Fawkes
I guess this might be more of a javascript problem now that I've understood it more.

Another thing I tried was adding a onClick to the modify button, but all that managed to do it make it execute another function or such before the onSubmit is run.

Code: Select all

"<form name='achievement_modify' method='post' onSubmit='return check_num();' action='".$_SERVER['PHP_SELF']."'>";
I thought about setting a flag somewhere globally in the javascript file, but that doesn't work as it is always reset back to initialization whenever the page loads something.

I should be able to insert this chunk of code into the javascript file that my php file includes right?

Code: Select all

	$("modify").click(function() {
		alert(this.id);
	});

Code: Select all

<input type='submit' name='modify' value='  Modify   '>
However nothing happens when the modify button is pressed, so I wonder if there's a huge logic problem going on here or a syntax thing.