open url from button using php - php

I need to make a button that:
Send to php script variable pressed=true when its pressed
open new tab with an adress
How can I do it without using java-script?

PHP can't cause events to happen in the client browser, you will need javascript for that. The button would have to submit the data to the PHP script, and then the script would display whatever output you like.
You could do this however
<FORM ACTION="phpscript.php" METHOD="POST" TARGET="_BLANK">
<BUTTON TYPE="submit" VALUE="1" name="pressed">Click</BUTTON>
</FORM>
This will open a new window(tab in FireFox at least)and you will have passed the value to your PHP script.
In your PHP script you check the value, and use a header to specify a new address:
if($_POST['pressed'] == 1 )
header("Location: http://www.whatever.com");

Related

PHP Form not posting the submit button correctly

I was doing an application form and the form is not working. When I click the submit button it doesn't even run the if statement to POST the variables.
My form tag looks like this:
<form method="post" enctype="multipart/form-data">
My submit button looks like this:
<button name="submeter" type="submit" class="btn btn-gfort">Submeter</button>
And the if statement in my form looks like this:
if(isset($_POST['submeter'])) {
I even tried to run a JS alert just to see if it actually enters the if statement but it doesn't. No console errors as well.Any help is appreciated
Have you added method="post" to the <form>-tag?
Well your codes looks fine. it seems you are submitting the form using post method on some php file. Please share the complete form element and the codes of the target file that you have defined in the action attribute of form tag.
the submit button sends the data to server and there you can process the data using any server side scripting language. your codes must look something like
<form action="process.php" method = "post">
<!-- your form controls -->`
<button name="submeter" type="submit" class="btn btn-gfort">Submeter</button>
</form>
then create another file on same location where html/php file containing this form is saved with name process.php, in which you can use following codes.
if(isset($_POST['submeter'])) {
// php codes
}
in case you want to subimt the form on the same page. make sure your file is php use action="#" in form tag and place the file of your webserver as you can not run php files directly from your filesystem but you need to run it through your webserver

How can a submit button made to be act as a link too?

I'm doing a Quiz project: The idea is to implement almost 25 questions in which each question occupies each HTML page with 4 radio buttons and a submit button and a reset button as well.On clicking the submit button it should take the user to the next page as well as submit the data to the server. How to achieve this dual behaviour?
I tried this:
<form action="cba.php" method="post">
<a href="abc.html">
<input type="submit" value="submit">
</a>
</form>
But this does only one purpose: Acting as a link without submitting the data.
If you just want to redirect the user after submitting the form, you can use :
header("Location: yourlink");
in the php script you called cba.php.
Otherwise, i'm not sure it is possible to redirect the user before sending him the php script page.
As mentioned, it would be a smoother experiance to handle this via ajax, but it can be acheived in just php by creating a redirect in the form processing code (as mentioned in comments and a current answer).
I believe your issue is with the fact that the same proccessing code (cba.php) will be called every step of the way, so you need a way for each quiz section to define the next section.
This can be done with a hidden field instead of the link code you tried:
<form action="cba.php" method="post">
<input type="hidden" name="next-page" value="abc.html">
<input type="submit" value="submit">
</form>
Then i cba.php, you redirect to the value contained in this hidden field:
//save the data from the form, then
header("Location: " . $_POST['next-page']);

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.

Why does my form submit to my php as a url instead of the actions in the php file

Ok I have a form with multiple submit buttons.
The coding on my php file has a header with a url depending on which form was entered. My issue is when I submit the form( no matter which button I use) the window that pops up is not the url action assigned to that button but the php file itself. What am I doing wrong?
the form starts of like this so that you can see if I directed it correctly
<form method="post" action="http://gamerzacademy.com/foodCYO.php" target="_blank">
<input type="text" name="uid">
<input type="submit" name="Dish1" value="Dish1" onclick="
this.disabled=true;
this.value='Gift Opened';
document.FreeFoodForm.submit();">
<input type="submit" name="Dish2" value="Dish2" onclick="
this.disabled=true;
this.value='Gift Opened';
document.FreeFoodForm.submit();">
etc......
now the php file starts like this
<?php
if ($_REQUEST['Dish1'] == "Dish1") {
header("Location: url1".urlencode($_POST['uid']));
}
else if ($_REQUEST['Dish2'] == "Dish2") {
header("Location: url2".urlencode($_POST['uid']));
}
else if ($_REQUEST['Dish3'] == "Dish3") {
header("Location: url3".urlencode($_POST['uid']));
}
.....etc
?>
You are posting the form through Javascript. The code doesn't know which button was clicked, so the value of that button isn't posted to the form. Therefor, your form cannot see which button was clicked. If you change the method to get, you will see which value do or do not get posted.
I think you don't need to post from Javascript at all. Just let the button do the posting. Only the name and value of the button that was clicked will be posted.
B.t.w., you disable the button, presumably because you don't want people to press the button twice, but in your setup they still can press any other button. I think it is wise to disable all of them.
Two things:
First, make sure the URLs you are sending in header are valid URLs.
Second, it looks like you have some whitespace before the <?php opening tag. Make sure there is no whitespace before the PHP opening tag. If there is, header won't work.
By doing the following in JavaScript:
this.disabled = true;
You effectively don't send its value to PHP.
A better idea might be an on submit handler in the form that prevents double submit.

Form Action file executing on the same page?

I have a form, on its action i have a php file, i want that when i click submit, it should first open the action php (means view in browser), and then start executing the code in it.
form tag is like this
<form action="myScript.php" method="post"></form>
right now what happen is, when i click submit, it stays on the same page and start executing the php file, when it is done then, then it shows the script file page in browser.
you can use jquery POST and do what ever you want:
jQuery.POST
Is the <input type="submit"> tags etc. inside the <form> tags. If not, then the input buttons are pointless, and just return false.
Your code should look like this:
<form action="myScript.php" method="post">
<input type="submit" />
</form>
Does this help?
Kai :-)
You have to execute the PHP in order to generate the page that is shown in the browser as a result of the POST. Even if your PHP is generating the page and then performing a long operation, output buffering and compression will defeat you, so you will need to turn them off. See How to flush output after each `echo` call? for other things you may have to do to get your output to appear immediately.

Categories