-3

Note: I'm asking less for a solution and more for help. I understand this question is probably basic and repetitive, but I'm less sorry than I am frustrated with how little other websites explain.

I am absolutely positive this question has been asked in a million variations because I've looked it up so many times, but the problem is how many times I've had to look it up and I still don't understand half of what I'm doing every time I attempt it. Pretty sure I've scraped the barrel with this one.

I have a process I'm running that calls several queries to a SQL database, and I'm doing them in PHP. Problem is, they're all run automatically when the page is opened, and that makes me so queasy it isn't funny. I'm trying to make a button that does not redirect to another page to call this process, be it by function or what-have-you. I'm trying to keep all of this on one page... Though I'm not averse to make it more pages.

I'm trying to avoid JQuery. My tools available are: JavaScript, PHP, and HTML. If I need to, I'm willing to convert all 8 or 9 queries into JavaScript to run it through button or even AJAX, but I'd like to keep it in PHP if I can. I know PHP and HTML run differently, but this is where I get confused on how best to combine them, and how to use JavaScript, and when to use JavaScript, and what GET and POST and SUBMIT are in relation to PHP, and just a million things. I'd like to know the BEST method AND the "you can do this with what you have already done", and WHY one is better than the other, if at all possible.

I'm sure 90% of this question is super repeated, but thank you in advance for your patience. I hate not understanding.

EDIT: I was asked for my code, and I can give a basic idea of it, without being able to give the code itself:

<?php
mysql_pconnect ('host', 'username', 'password');
mysql_select_db('database'); 
// the company I work for has so far refused to let me upgrade our website, so I
// am trying to keep it the same so that half of it isn't new and the other
// super outdated. Believe me, I'm pushing.

$query0 = "TRUNCATE table templateTable;";

$query1 = "INSERT INTO templateTable
(item1, item2, item3, item4...)
SELECT
table1.itemA, table2.itemB, table1.itemC, table2.itemD...
FROM table1, table2
WHERE table1.itemA = table2.itemQ;";
// there are a LOT of items in this query, like 100

$query2 = "UPDATE templateTable, table1
SET templateTable.itemX = \"thing\",
CASE
WHEN table1.itemAB = 1
THEN itemX = 'THING1';";

$query3 = "UPDATE templateTable, table2, table1
SET templateTable.itemY = 
CASE
WHEN table2.modelNumber = table1.modelNumber
THEN itemY = table2.modelNumber
ELSE
itemY = table1.otherInformation;";

// these queries are using data from two tables to fill an inventory 
// template table that will only be filled and edited through queries. there 
// is no manual editing of this table- just queries.

/*
I'm hoping you get the gist of this because there's 5 other queries even 
longer and more complicated @.@ I have a procedure in MySQL to do this but my 
coworkers are nervous about running the procedure, so I'm trying to make it more 
friendly by allowing it through the website. This was requested of me
*/
?>

Long story short: I'm trying to run a PHP function on the same page with HTML through a button, the php has 8 queries being run to a MySQL database, and I don't really understand any of this.

Kitfoxpup
  • 265
  • 1
  • 2
  • 11
  • 2
    Why are you avoiding jQuery? Makes it very easy to light up AJAX requests, *which is what you're looking for*. [Here are the basics](http://jayblanchard.net/basics_of_jquery_ajax.html) of calling a PHP file with AJAX. – Jay Blanchard Aug 10 '15 at 16:06
  • Because I'm not 100% sure how JQuery runs, and this isn't the focus of today's workload. HOWEVER if I need to, I'll learn it. I'm just trying to avoid making this process into an ordeal. Again, I'm really open to trying to learn the process behind this question, because I don't want just blatant answers on old questions left to the archives. Edit: I guess this is part of the problem. I don't even understand AJAX fully, or JQuery, or how all of this connects. Which has to be the most frustrating part. – Kitfoxpup Aug 10 '15 at 16:07
  • 5
    jQuery is just JavaScript which allows you to shorten your code and make certain things very easy. – Jay Blanchard Aug 10 '15 at 16:08
  • Quick question- do you know I've replied to you if I don't tag you? All these forum sites work differently. So basically JQuery is just javascript? Does that mean AJAX is my best solution? Keeping it in PHP, the whole shebang? I feel like I've opened Pandora's Box. – Kitfoxpup Aug 10 '15 at 16:11
  • It depends, I saw your reply this time. Yes, you send a request via AJAX to a PHP script on the server. The PHP script responds with the data that you need. See the link in my earlier comment. – Jay Blanchard Aug 10 '15 at 16:12
  • I missed the link, apologies for that. I'll take a look, but AJAX has confused me for ages. I still don't really understand what it is and why I need it, with the whole client-side thing. I think maybe I'm just missing something obvious that ties the whole thing together, and after that I'll have a lightbulb moment or something. Thank you by the way, I knew this question was likely to either get ignored or just downright annoy people – Kitfoxpup Aug 10 '15 at 16:15
  • (The reply system will work out that you intended to reply to someone, even if you do not tag them, if they are the only respondent so far. However it is best to tag people by default. For readers here we don't need to, since the original poster _always_ gets a notifications on their posts). – halfer Aug 10 '15 at 16:19
  • Have a look at the link and see if that demystifies things for you. – Jay Blanchard Aug 10 '15 at 16:20
  • Can we see your code so far? I agree that jQuery is a good approach, but AJAX is pretty easy without. See [this helpful site](http://youmightnotneedjquery.com/). – halfer Aug 10 '15 at 16:21
  • 1
    @halfer I can give you an idea of what my code is at this moment, which is raw and definitely not safe. I'll edit my main question- thank you! Also, reading into first link and will look into yours, these are fantastic, I'm understanding more than I have been able to in three months looking into this. – Kitfoxpup Aug 10 '15 at 16:28
  • This is a really broad question IMHO and you may want to start with the very basics like HTTP works and what runs where (server, client, …) and therefore has access to which ressources (database, displayed webpage, …). The debugging tools of the Browser, either builtin or via plugins, can reveal what is sent back and forth between client and server and help to understand what is going on under the hood. – BlackJack Aug 10 '15 at 16:29
  • Why would you need AJAX: because "`I'm trying to make a button that does not redirect to another page to call this process`". You don't want to redirect/reload, but you still want to call a PHP script. AJAX allows Javascript to call this page without reloading. –  Aug 10 '15 at 16:29
  • Please do edit the question, yes. It doesn't matter if the code is raw - let's see it (I did not downvote but it is probably why people voted that way). It's usually good advice to add this in the first edit, so people can start offering ideas. We don't do code review here - there is a separate site for that - but if you can show what you have it'll probably be on-topic here, and you'll get a bit of review anyway! – halfer Aug 10 '15 at 16:37
  • @caCtus yes and no. I don't even fully understand what I'm trying to do, and I'm pretty sure what I am asking for isn't even the best method. I want to know the best method and understand why it's the best method. If I need to learn JQuery, I will. If I need to use AJAX, I will. If I need to translate the whole dang thing to Javascript, I will. If I NEED to go to another page or redirect, I will. I just don't understand it is what the problem is. – Kitfoxpup Aug 10 '15 at 16:37
  • @BlackJack I know, I'm sorry it's so broad, and that's the issue I'm having with all of the previous questions asked about making buttons that call PHP functions. I'm not sure it's appropriate, if I should be doing it, and if it's outdated or why it's outdated. I'm working on an ancient website I did not originally design as well as a database that's just as fragmented/horrific, but I'm not allowed to re-do either currently. That may be part of the problem. I'm trying to learn but I don't even know where to ask the right question... – Kitfoxpup Aug 10 '15 at 16:39
  • 1
    There is not a heckuva lot of code to go on in your edit but if you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 10 '15 at 16:41
  • @JayBlanchard I know, I apologize- it literally just is that in simplified form and super repetitive. A million UPDATE calls and setting fields in the table- it is being completely filled with data from other tables. Also, I'm aware of the deprecation of the website, it's ancient and as you mentioned not maintained, and I'm pushing to just outright re-do the whole dang thing, but my boss won't allow it because he is positive it's not necessary. I'm worried (probably unnecessarily) that if I start using the new PHP, the site's going to freak out with the million other scripts it's using. – Kitfoxpup Aug 10 '15 at 16:49
  • 1
    I totally get that, no need to change at this point. If they decide to update PHP it will become a huge problem @Kitfoxpup Give me a shout out on Twitter and I'll try to help you get through the initial stages of this, because the current discussion is way too broad for StackOverflow. – Jay Blanchard Aug 10 '15 at 16:58
  • @JayBlanchard I'm either going crazy or I can't find your twitter handle. I was sort of afraid this would get broad as heck. – Kitfoxpup Aug 10 '15 at 17:20

2 Answers2

1

I'm trying to run a PHP function [...] through a button [...] that does not redirect [or reload]

First you need to understand when is PHP executed, when is HTML rendered and when is Javascript executed.
Look for "difference between cliend-side and server-side" on the Internet, you'll find good explanations. It seems to be a big deal when you're a beginner but once you understand that, knowing when you have to use PHP, if and when you have to use Javascript, and which is the most accurate, is quite easy to get.

In a few words :

PHP is a server-side language. When you load a webpage, your browser (client) sends a request to the server. The server executes the PHP code, generates some HTML (in our case), and sends this HTML back to the browser.

HTML is rendered client-side. This is your browser that reads and renders HTML.

Javascript is a client-side language. Javascript code goes with the HTML sent from the server to the browser. The browser is able to execute Javascript.


In order to execute PHP code when you click on a button, there is 2 solutions :

  • Either this button is a link, or is part of a form. When you click on it, the page is reloaded (you can send it GET or POST parameters if needed), then your PHP code can be executed.
  • Either you use Javascript and AJAX, and you don't need to reload your current page. Your browser, with Javascript, will send another request to the server "in the back"*, and the server will be able to run some PHP code without you having to reload anything.
    *(looking for an English expression that I don't know)

A quick word about jQuery : jQuery still IS Javascript. jQuery is a Javascript library, basically allowing you to have some cleaner, clearer and easier to write code (IMO). But, as halfer mentions in a comment, you don't need it everytime. Other libraries exist, and sometimes, for what you need, "pure" Javascript is enough.

  • I think this, as well as everything else going on in my question's response novel, has set a good groundwork for me to better understand. Thank you very much- I greatly appreciate the patience and the help I've received. ^_^ – Kitfoxpup Aug 10 '15 at 17:48
0

If you place your button into a form with a hidden field like so:

<form>
    <button type="submit">Run Queries</button>
    <input type="hidden" name="submitted" value="1">
</form>

And use and IF statement around your queries:

if ($_GET['submitted'] === '1') {
    // Run queries
}

You get the results it seems you want.

Very simplified: On an initial page load, the queries will be bypassed by the IF statement, which checks the submitted get parameter. When you press the button (submit the form) the submitted parameter will become 1

halfer
  • 19,824
  • 17
  • 99
  • 186
Mike Wanush
  • 104
  • 3
  • 1
    Not sure OP wants to reload the page. –  Aug 10 '15 at 16:37
  • What does the Hidden tag do, I've not yet seen this anywhere I've looked at tags. I'm also not even 100% sure on how $_GET works, except that the page will be reloaded and the URL becomes something like "page.php?submitted=true", and anything checking for that through the $_GET will be affected. I think I'm just flustered at not having learned all of this structurally. – Kitfoxpup Aug 10 '15 at 16:44
  • 2
    @Kitfoxpup don't get distracted with this, this is not likely going to be what you're after ultimately. – Jay Blanchard Aug 10 '15 at 16:44