0

Is there a way that when I click a button it will execute a bunch of php codes?

For example;

<input type="button" value="Submit Order" class="minibutton" **onClick="executePHP"**>

This button object right here if I click it this php codes will be executed.

    include("../includes/connect.php");
    include("../includes/generateTimestamp.php");
    include("../includes/knowthename.php");


    $pid=$_POST['submit_id'];
    $pname = $_POST['submit_name'];
    $total = $_POST['submit_total'];
    $quantity = $_POST['submit_qty'];
    $price = $_POST['submit_price'];
    $unit = $_POST['submit_unit'];
    $change = $_POST['submit_sukli'];
    $payment = $_POST['submit_payment'];
    $count = $_POST['count'];
    //explode
    $newpid = explode(";",$pid);
    $newpname = explode(";",$pname);
    $newquantity = explode(";",$quantity);
    $newprice = explode(";",$price);
    $newpriceunit = explode(";",$unit);


    $sql = mysql_query("INSERT INTO sales (sales_date, total_price, emp_id) VALUES ('$timestamp','$total', '$employee_id')");
    mysql_query($sql);
    $id_gen = mysql_insert_id();
    $count = $count - 1;
    while($count = -1){

            $product_id = $newpid[$count];
            $product_name = $newpname[$count];
            $product_quantity = $newquantity[$count];
            $product_price = $newprice[$count];

            $sql2("INSERT INTO sales_details (sales_id,product_id,sales_qty,unit_price,net_price) VALUES ('$id_gen','$newpid[$count]','$product_quantity[$count]',
            '$product_price[$count]','$newpriceunit[$count]')");
            mysql_query($sql2);         
        }
    header('Location: prompt.php?x=5');

    }else{
        echo 'Nothing here!';
        }

I wanna make the onclick function as a breaker that if I click it, It will unbreak and continue to execute those bunch of codes,

If there is no way I can do that, is there other ways?

function isset won't work because the data will be lost(arrays) because the original data came from $_POST. My hope is the onclick event but i don't know how.

Please enlighten me wizzards of computer!

1
  • I would say this can likely be done with a jQuery/Ajax request -- api.jquery.com/jQuery.ajax Commented Jun 7, 2012 at 23:43

3 Answers 3

3

You can only attach JavaScript to browser events. If you want to execute server-side code such as PHP, you need to make a call to the server. In your situation, forget javascript and just POST your form to your PHP page.

<form method="post" action="file.php">

  <!-- form inputs -->

  <input type="button" value="Submit Order" class="minibutton" />

</form>

then put your code in file.php

Edit

Put all your post data in a hidden form fields. Then when you click the button, it will resubmit the post data.

foreach($_POST as $key => $val) {
    echo '<input type="hidden" name="'.htmlspecialchars($key).'" value="'.htmlspecialchars($val).'" />'."\n";
}

If you have a multi-dimensional post array, just serialize() it, stick it in 1 field

echo '<input type="hidden" name="data" value="'.htmlspecialchars(serialize($_POST)).'" />'."\n";

then unserialize() it on the other side.

$_POST = unserialize($_POST['data']);
Sign up to request clarification or add additional context in comments.

3 Comments

Like I said, I cannot do a POST and get it on isset because the file data is also came from another POST method. If I do that then all the data will be erased and it is an ARRAY.
Okay, the original data came from sales.php using POST. The current page I'm in is checkorder.php. I've display all the data that came from sales.php. Now I made a button that if that button gets clicked, then it would execute a block of php to put the data in the database.
This might be the answer I'm looking for, Its my first time to encounter this serialize() function. I will read more about it and update you if this works. Thanks a lot ddlshack!
0

Using jQuery:

$('.minibutton').on('click', function(e) {
    // do some ajax stuff here 
});

Examples: http://api.jquery.com/jQuery.ajax/

1 Comment

Its a good solution but I still haven't tried ajax or jquery, I would try it in the future. I'm doing this project with the basic php and js if there is.
0

If your .php needs values that comes from the clicked element, no you dont. If your code is just a bunch of php on its own, irrelevant to client requests, you could echo it in a js function and call that on click.

2 Comments

I guess that could work but the thing is how is it okay to put a php codes inside a JS function? I could be getting errors by this but I will try it to see if it works
actually no -- i meant you call your php -while- on server and echo a possibly produced js code BY the .php (the only thing callable by js without hitting the server) - you cannot call .php while on client's side; only a result of .php echo (js/html), just echoed in your function..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.