I have an index.php and output.php
I need two buttons both linked to output.php but to different functions and I can't get it work. Here is my code:
In index.php:
<?php
include 'output.php';
if (isset ($_POST ['choice3'])){
choice3();
}
if (isset ($_POST ['choice4'])){
choice4();
}
?>
<form method="post" action="output.php">
<input type="submit" name="choice3" id="choice3" value="Choice 3">
<input type="submit" name="choice4" id="choice4" value="Choice 4">
</form>
In Output.php,
function choice3(){} and function choice4(){}
Each function have different output.
But now, when I click the button, nothing shows up.
Thanks for help!!
Replace your index.html.php and output.php with this
Index.php
<form method="post" action="output.php">
<input type="submit" name="choice3" id="choice3" value="Choice 3">
<input type="submit" name="choice4" id="choice4" value="Choice 4">
</form>
Output.php
<?php
if (isset ($_POST ['choice3']))
{
/ body of the choice 3 function without the function signature
}
elseif(isset ($_POST ['choice4']))
{
/ body of the choice 4 function without the function signature
}
?>
Just Replace your code with this
<?php
include 'output.php';
$choice = $_POST['choice'];
if ($choice == 'choice3') {
choice3();
} else if ($choice == 'choice4') {
choice4();
}
?>
<script>
function make_choice(choice) {
document.getElementById('choice').value = choice;
document.choice_form.submit();
}
</script>
<form method="post" action="" name="choice_form">
<input type="hidden" name="choice" id="choice" value="" />
<input type="button" name="choice3" id="choice3" value="choice3" onclick="make_choice(this.value);">
<input type="button" name="choice4" id="choice4" value="choice4" onclick="make_choice(this.value);">
</form>
Related
im currently trying to get a value from a textbox but im always getting undefined array key.
<head>
<!-- Links -->
<link rel="stylesheet" href="./css/vouches.css">
</head>
<body>
<div class="content">
<form method="get"><input type="text" id="oderid" name="oderid" placeholder="oderid / invoiceid"></form>
<form method="post"><input type="submit" name="button1"class="submitbtn" value="Button1" /></form>
<?php
if(array_key_exists('button1', $_POST)) {
button1();
}
function button1() {
$id = $_GET['orderid'];
echo $id;
}
?>
</div>
</body>
</html>
in the part is where the stuff happens. I hope i can get some help, im really new to php
When clicking the button it should echo $id, better said the textbox input
Don't use both POST and GET; pick one.. Like this:
<form method="post">
<input type="text" id="oderid" name="orderid" placeholder="oderid / invoiceid" />
<input type="submit" name="button1" class="submitbtn" value="Button1" />
</form>
<?php
if(array_key_exists('button1', $_POST)) {
button1();
}
function button1() {
$id = $_POST['orderid'];
echo $id;
}
?>
Also take care of your spelling, orderid is not the same as oderid.
would it be possible to have a html/php template on index.php say for example (a news webpage template and then anyone can edit the title, paragraphs only, then on submit it then sends the webpage with the data stored to a paste bin like url so who ever visits that url say http://localhost/news/jjeh3bndjks they would only be able to view to content and not edit.
I would like to use something like this
<?php
if ($_POST) {
$pasteID = uniqid();
$paste = fopen("pastes/".$pasteID.".php", "w");
$contents = $_POST['pasteContents'];
fwrite($paste, $contents);
header('Location: /pastes/'.$pasteID.'.php');
}
?>
<form action="" method="POST">
<input type="text" name="pasteContents" placeholder="write here" />
<button type="submit" tabindex="0">submit</button>
</form>
but for some reason when i add another input box or try to send anymore data it fails or just gives me the last input given is there a way to send a whole page this way?
any help would be appreciated
You can use file_get_contents with the following code:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
parse_str(file_get_contents('php://input'));
echo param1 . '<br />' . param2;
} else {
?>
<form method="post">
<input type="text" name="param1" value="param1" />
<input type="text" name="param2" value="param2" />
<input type="submit" value="submit" />
</form>
<?php } ?>
(You can test it here)
Although, I did success to use $_POST too:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo $_POST['param1'] . '<br />' . $_POST['param2'];
} else {
?>
<form method="post">
<input type="text" name="param1" value="param1" />
<input type="text" name="param2" value="param2" />
<input type="submit" value="submit" />
</form>
<?php } ?>
Here
I'm doing php that is textbox a value empty it will open a alertbox (I'm using javascript in here )
this is my code
<?php
include('config.php');
if(isset($_POST['submit'])){
$username=$_POST['username'];
?>
<script>
function validate(){
if(document.forms[0].username.value==""){
window.alert("You must enter both values");
return false;
}
}
</script>
<?php
}
?>
<html>
<div><p>Member Profile</p>
<form action="testing.php" method="POST" onsubmit="return validate();">
Username<br>
<input class="user" type="text" name="username" id="username" /><br>
<input type="submit" name="submit" value="register" />
</form>
</div>
</html>
The problem is i have to click 2 times before the alert show
please help me to solve this problem
It's because the script is inside the php if(isset){} block, one click submits the form, which generates the script and then it works the second time.. try this setup instead:
<?php
include ('config.php');
if (isset($_POST['submit']))
{
$username = $_POST['username'];
}
?>
<html>
<head>
<script>
function validate () {
if (document.forms[0].username.value == "") {
window.alert("You must enter both values");
return false;
}
}
</script>
</head>
<body>
<div>
<p>
Member Profile
</p>
<form action="testing.php" method="POST" onsubmit="return validate();">
Username
<br>
<input class="user" type="text" name="username" id="username" />
<br>
<input type="submit" name="submit" value="register" />
</form>
</div>
</body>
</html>
Edit:
I've moved the script tag inside the head tag. I'm not sure if there are any implications for having the script outside but just to be sure I've moved it.
2nd Edit: (OCD is kicking in)
I've added body tags, not sure if you copied and pasted this code but it looked weird to me :)
I want to have a php contact form, that when submitted (if no errors), will refresh the same page, and remove the contact form, and replace it with some text e.g. "thank you for contacting us".
Is there a best way to do this?
You could always do something like this:
<?php
$posted = false;
if( isset($_POST['submit']) ) {
/* Process $_POST */
/* Do your things */
/* Set a variable hinting if a post has been done */
$posted = true;
}
?>
<!DOCTYPE html>
<html>
<body>
<?php if( $posted ): ?>
<form method="post">
<input name="foo" />
<input name="bar" />
<input name="car" />
<input name="submit" type="submit" value="Submit" />
</form>
<?php else: ?>
<h1>Thank you for contacting us!</h1>
<?php endif; ?>
</body>
</html>
Do it this way.
<?php
$showform=true; //dispaying the form for the first time
if(isset($_POST['submit'])){
//check for errors
if($errors){
$msg="errors";
$showform=true; // if found errors, setting error msg and displaying the form
}else{
//process the form
$showform=false; //if no errors, setting success msg and hiding the form
$msg="SUccess";
}
}
if(isset($msg)){
//display the success/error msg
echo $msg;
}
if($showform==true){
//your form code
<form method="post">
<input name="foo" />
<input name="bar" />
<input name="car" />
<input name="submit" type="submit" value="Submit" />
</form>
}
?>
Can anyone please help me on this:
I am trying to add a classname/ change the css of div using jquery; after I click submit on the form. Means, after clicking submit and when the page reloads this classname/ changes should stick to the target element. I am using php to get the values of form submitted. (no ajax, but if necessary I can modify my code). I tried to do this by jquery but when the page reloads the changes are gone !
Thanks.
update: Here is the sample code(so.php itself) I am working on. after form is submitted and page is reloaded; say I want to add a class to input that is clicked and manage the style in CSS:
<html>
<head>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input name="Load" type="submit" value="Google" />
<input name="Load" type="submit" value="MSN" />
<input name="Load" type="submit" value="Yahoo" />
</form>
<div id="container">
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
</body>
</html>
update2: I tried the logic provided by #NoPyGod and it worked. But here is the thing. I am trying to add a class to the button that is selected as well as update the text in the div with id "hello" with the value of input that is clicked or in active state once the form is submitted and page is reloaded. This is the one I ended up but once form is submitted
all the inputs are having the same class name as well the text in div is also not updated.
<html>
<head>
<?php if (isset($_GET['Load'])): ?>
<script>
$(document).ready(function() {
$('#hello').text('Hello');
});
</script>
<?php endif; ?>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load" type="submit" value="Google" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load1" type="submit" value="Rediff" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load" type="submit" value="Yahoo" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load1" type="submit" value="Sify" />
</form>
<div id="container" >
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
if (isset($_GET['Load1'])) {
$value = $_GET['Load1'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
<div id="hello"></div>
</body>
</html>
When the page is submitted, set a variable like so
$was_submitted = true;
Then in your HTML do something like this
<div id="myid" class="<?php if ($was_submitted) { echo "some_class"; } ?>">
Or if you absolutely insist on using jQuery to change it, include this in your html --
<?php if ($was_submitted): ?>
<script>
$(document).ready(function() {
$("#myid").addClass("some_class");
});
</script>
<?php endif; ?>
Updated:
<html>
<head>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input name="Load" type="submit" value="Google" />
<input name="Load" type="submit" value="MSN" />
<input name="Load" type="submit" value="Yahoo" />
</form>
<!-- I changed this line below -->
<div id="container" class=<?php if (isset($_GET['Load'])) { echo "some_class"; } ?>>
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
</body>
</html>