knowing if a html button is clicked using cakephp in the view - php

Here is a part of my cakephp view code:
</fieldset>
</form>
<?php
$account = new Account();
$account->insertPos();
?>
<button id="button" name="button" onClick="assign();" value="save"> Save Changes </button>
</body>
I need the php part to be executed only if the button is clicked, how can i check that the button is clicked inside my view and execute this php code...
Thank...

There's no way to do this in the same PHP request. By the time the user sees the button in order to click it, the page has finished rendering, the request is over, and PHP's job is complete.
You'll need to send another request to the server to make this happen, either via submitting a regular form, or via Ajax. Here's a simple non-Ajax solution (may require tweaking to fit with the rest of your code):
<div id="result"><?php
if(isset($_POST['newaccount']))
{
$account = new Account();
$account->insertPos();
}
?></div>
<form id="newaccountform" method="post">
<input type="submit" id="button" name="newaccount" value="Save Changes">
</form>

Related

How to give one file uploaded value to input (file) tag on second page?

i use a form in which i get a file from user and submit it with button and take the values to another page with help of post(method). On the other page i have some business to do with that file beside that i have another form on that page. Now i want when submit that form (as it execute the php self script) so i want beside the new values i get that same file again. i try different technique like putting the tmp name in a variable but that doesnot work and even i create another file button in the form of that page but i am unable to give it a value. In short i need idea how to get the file on submittion of that second page form which is send from first page ?
For simplicty let me show you an example so you understand my problem,
//the first page form
<form method="post" enctype="multipart/form-data" action="uploadExcelData">
<label for="csv_file">Upload CSV File</label>
<input type="file" id="csv_file" name="csv_file" />
<input type="submit" name="upload" class="btn btn-primary"
value="upload" />
</form>
//on submit it goes to another page which have this php code and html form
<?php
$file = $_FILES['csv_file']['tmp_name'];
if (isset($_POST['save'])) { // the second form on submit
echo $file; //this show nothing
}
?>
<form method="post" enctype="multipart/form-data">
<input type="submit" name="save" class="btn btn-primary btn-block"
value="Upload" />
</form>
keeping above code in mind i hope somebody will understand the problem and suggest me something but i just summarize my code too much the second form also have many combobox etc and also i tryed to have a second file tag in second form but the problem is i cant even assign the file value to it so that it is selected automatically.
i want to get the file as output which have that shitty excel data!!

exec a php to execute shell script

I have a php script like this.
This doesn't run my script nor print anything on the click of the button. How to do that
Also if you have a pointer to php 101 online please let me know
<?php
if ($_GET['run']) {
# This code will run if ?run=true is set.
ob_start();
passthru("./check_ddts.sh ".escapeshellarg($_POST["CSCui21515"]));
$data = ob_get_clean();
}
?>
<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
<button type="button" onclick="?run=true">Click Me!</button>
Your button is not actually doing anything. The problem is that your onclick attribute should contain javascript which will run when the button is clicked, not a redirect location. Use a form with a submit button instead like this.
<form action="" method="get">
<input type="submit" name="run" value="Click Me!" />
</form>
I notice in your question that you are also using post data in handling the form, in which case the above will not work. You need to submit the POST data at the same time as you run the script.

How to post form variables with javascript, just like a type="submit" button

I'm a noobie programmer and I wonder how to properly submit a form with javascript.
I made some test code to show you what I mean:
<?php
if (isset($_POST['message']))
{
echo $_POST['message'];
}
?>
<script>
function formsubmit()
{
document.getElementById('form').submit();
}
</script>
<form id="form" name="form" action="" method="post">
<input id="message" name="message" value="hello world">
<input id="submit" name="submit" type="submit">
</form>
Click me<br/>
<input type="submit" onClick="formsubmit()" value="Click me">
When you push the "submit" button inside the tags - the php code will echo "hello world".
When submitting the form with JS the values won't post to the page. What am I doing wrong?
Thanks in advance.
I've searched the whole afternoon for a solution, but cause of my lack of knowledge about programming I failed to find it.
Believe it or not, but the main problem lies here:
<input id="submit" name="submit" type="submit">
If a form contains an input element with name (or id) of submit it will mask the .submit() method of the form element, because .submit will point to the button instead of the method. Just change it to this:
<input name="go" type="submit">
See also: Notes for form.submit()
The smaller problem is here:
Click me<br/>
An empty anchor will just request the same page again before calling formsubmit(). Just add href="#".
The problem here is that the id and name of the input element on your form is called submit.
This will mask the submit function for the form. Change the name and id and you will be able to use javascript to submit the form.
try setting the href of the to '#'. I would guess what is happening is that by clicking on the link, it submits the form and immediately changes the url to the same page you are on cancelling the form submit before it has a chance to go.

Redirect to a php page based on submit button clicked

I have two submit buttons in a php page. Based on the submit button clicked, I wish to redirect user to different php page. How that can be done?
<form>
<input type="submit" value="Go To Page1.php">
<input type="submit" value="Go To Page2.php">
</form>
Assuming you don't have any shared input boxes, you can just do something like this, or use simple links.
<form action="http://example.org/Page1.php">
<input type="submit" value="Go To Page1.php">
</form>
<form action="http://example.org/Page2.php">
<input type="submit" value="Go To Page2.php">
</form>
If you have additional input elements, I suggest looking into this solution. Relevant code sample:
<input type="submit" name="submit1" value="submit1" onclick="javascript: form.action='test1.php';" />
Just use a set of a tags inside the form:
<form>
<!-- Other Inputs -->
<div id="redirects">
Go to page 1
Go to page 2
</div>
</form>
If you need to send certain information along with your redirect, keep your current form and have a condition at the top of your file:
<?php
// You will need to send the $_POST data along with the redirect
if(isset($_POST['submit1']))
header('Location: page1.php');
else if(isset($_POST['submit2']))
header('Location: page2.php');
// Continue with the page...
?>
To send the $_POST vars along with the redirect, check this other SO post.
You don't need a form for this, neither input fields. Use <a> tags instead. You can style them with CSS to look like a button if you want that.

using multiple submit buttons in a form

I have a form with two button .I want to redirect to two different pages when i click these button.On the click event of both buttons i am executing a same function.I want that the function should be executed first then it should be redirected to respective page
you could change action of form on the fly during submitting (and answer varies depending on using jquery, pure javascript etc.)
but I recommend using php for that task
<input type="submit" value="Edit" class="spare" name="edit">
<input type="submit" value="Send" class="spare" name="send">
and then:
if (isset($_POST['edit']))
...
elseif(isset($_POST['send']))
...
Try this:
<input type="button" value="Edit" class="spare" onclick="check('edit')">
<input type="button" value="Send" class="spare" onclick="check('send')">
Where with check() will add the action to the form and then submit it:
function check(action){
var form = document.getElementById('formId');
form.action = 'http://needed/url/' + action;
form.submit();
}
Hope it solves your problem :)

Categories