Basically what I'm trying to achieve is a simple PHP on/off where I have PHP code which is set to 0 but have the ability to change it to a 1 or 0 from a form.
PHP file| $settingon = 1 //1 for active, 0 for inactive
HTML file| <form action="php/settingfile.php">
<input type="radio" name="settingon" value="1"> Active<br>
<input type="radio" name="settingon" value="0"> Inactive<br>
<button type="submit">Save Settings</button>
</form>
If you want to disable the input:
<form action="php/settingfile.php">
<input type="radio" name="settingon" <?php $settingon? "":"disabled" ?>> Active<br>
<button type="submit">Save Settings</button>
</form>
or create a class that displays the input as you want:
<form action="php/settingfile.php">
<input type="radio" name="settingon" class="<?php $settingon? "active":"inactive" ?>"> Active<br>
<button type="submit">Save Settings</button>
</form>
Related
I have an html-form which includes some information to be sent to the php-script. The type of an input of this information is "button". It is important that all the values are saved.
<form name="CoordinatesReceiving" method="get" action="MainScript.php">
<p> Insert the coordinates of the point: </p>
<label> Insert X <input type="button" name="xCoordinate" id="xCoordinate" value="1"> </label> <br>
<label> Insert R <input type="button" name="radius" id="radius" value="5"> </label> <br>
<input type="submit" name="send" value="Send.">
</form>
The form has to "remember" that I pressed these buttons and send them to the server.
How can I implement this?
An example for the x-button:
<label for="xCoordinate">Insert X</label>
<input type="button" name="xCoordinate" id="xCoordinate" onclick="this.value=1">
<form id="test" method="post" action="getValue.php">
<input type="submit" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="submit" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
</form>
I want to know how to get the value of customized attributes of between several radio buttons like example above by using php.
How can i get the value of customizedValue1 and customizedValue2 in php?
Thanks
You can't access directly from PHP to this values, you need to pass them as AJAX POST values to the PHP file like this:
FORM
<form id="test" method="post" action="getValue.php">
<input type="radio" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="radio" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
<button type="submit"> Submit </button>
</form>
JS
$('#test').on('submit',function(){
var customizedValue1 = $('#test input[name=sample]:checked').attr('customizedValue1');
$.post('getValue.php',{'customizedValue1':customizedValue1});
});
On getValue.php you can access to the value:
echo $_REQUEST['customizedValue1'];
If they are connected to eachother somehow. You can also use the values as an array in html form
<form id="test" method="post" action="getValue.php">
<input type="text" name="data[A][customizedValue1]" value="value1" />
<input type="text" name="data[A][customizedValue2]" value="value2" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])){
$customizedValue1 = $_POST['data']['A']['customizedValue1'];
$customizedValue2 = $_POST['data']['A']['customizedValue2'];
echo $customizedValue1;
echo $customizedValue2;
}
?>
I've a form that will require user to choose/click at least two buttons in order to submit the form
<button type="button" name="Investor-agree-one">I AGREE</button>
<button type="button" name="Investor-agree-two">I AGREE</button>
<button type="button" name="Investor-agree-three">I AGREE</button>
<button type="button" name="Investor-agree-four">I AGREE</button>
<button type="button" name="Investor-agree-five">I AGREE</button>
How do I validate the form using php that at least two buttons are selected and redirect user to one page, if not redirect to another page? So basically is like:
if(buttonSelected>=2){
goto this page
}else{
goto another page
}
How do I indicate whether the button is being selected in the first place using the button elements?
Thats pretty easy,
Give your buttons all the same "name", and a unique value
so lets say we have this button tag:
<form method="post">
<button name="somebutton" value="buttonone">
<button name="somebutton" value="buttontwo>
<button name="somebutton" value="buttontwo">
</form>
Your php should then look something like this:
<?php
$button = $_POST['somebutton'];
if($button == "buttonone"){
//do button 1 stuff, in your example:
header('location: someurl.php');
}
if($button == "buttontwo"){
// do button 2 stuff
}
?>
You may use checkbox instead of button, so your code may like this:
<?php
if(isset($_POST['agree_one'])) {
// do something
}
?>
<form method="post">
<label>
<input type="checkbox" name="agree_one" value="1"/>
I Agree
</label>
<label>
<input type="checkbox" name="agree_two" value="1"/>
I Agree
</label>
<label>
<input type="checkbox" name="agree_three" value="1"/>
I Agree
</label>
</form>
But if you just want to count how much user has selected the agree checkbox, you may want this code:
<?php
if(isset($_POST['agree']) && count($_POST['agree']) > 2) {
// do magic
}
?>
<form method="post">
<label>
<input type="checkbox" name="agree[]" value="1"/>
I Agree
</label>
<label>
<input type="checkbox" name="agree[]" value="1"/>
I Agree
</label>
<label>
<input type="checkbox" name="agree[]" value="1"/>
I Agree
</label>
</form>
I have two buttons in my form, one is for answer a question and the other is for copy the question.
<div id="question">
<?php echo($question->content) ?>
</div>
<form action="script.php" method="GET" id="question">
<input type="text" name="question">
<button id="answer" onclick="document.getElementById('question').submit()">Answer the question</button>
<button id="copy" onclick="document.getElementById('question').submit()">Copy the question</button>
</form>
The URL of script.php look now like:
script.php?question=sometext
Now I want that when you click at the copy button the URL looks like this:
script.php?question=sometext©
And for the answer button:
script.php?question=sometext&answer
EDIT:
There are much answers where is said: "use <input type> instead of <button>"
The problem is that I can't use a input field as button because the button is outside my form. And I can't put it inside my form
What you can do is to use one hidden field and change it's name according to the pressed button. Something like the following:
<div id="question">
<?php echo($question->content) ?>
</div>
<form action="script.php" method="GET" id="question">
<input type="text" name="question">
<input id="action" type="hidden" name="" value="">
<button id="answer" onclick="document.getElementById('action').setAttribute('name','answer'); document.getElementById('question').submit()">Answer the question</button>
<button id="copy" onclick="document.getElementById('action').setAttribute('name','copy'); document.getElementById('question').submit()">Copy the question</button>
</form>
Although this would give you the result you want at the url, it would be more appropriate to have as the hidden's field name the "action" and to change it's value to "copy" or "answer" through javascript.
Try to make two forms, with a hidden input field with the values. Then you get the extra parametrt in your url when submitting
Change your form to the following
<form action="script.php" method="GET" id="question">
<input id="question" type="text" name="question">
<input id="answer" type="submit" name="answer" value="true">
<input id="copy" type="submit" name="copy" value="true">
</form>
url:
script.php?question=hello©=true
Then you can check
if(isset($_GET['answer']) && $_GET['answer']=="true"){
//answer action
}
if(isset($_GET['copy']) && $_GET['copy']=="true"){
//copy action
}
For some reason the radio button value from my form is not being passed to the form PHP script on some web browsers when using the Twitter Bootstrap framework.
Form:
<form action="" method="post">
<li class="stream-item stream-header search-input-item">
<span class="add-on"><i class="icon-search"></i></span>
<input type="text" class="input-xxlarge" id="appendedInputButton" autocomplete="off" name="q">
<button class="btn btn-primary" type="submit">Search</button>
</li>
</div>
<p></p>
<label class="radio">
<input type="radio" name="opt" value="1" checked>
Option 1
</label>
<label class="radio">
<input type="radio" name="opt" value="2">
Option 2
</label>
</form>
This the PHP code for collecting the variable is one simple line:
$option = $_POST['opt'];
This method works absolutely fine with Chrome but doesn't work in IE, Firefox or Opera.
Can anyone shed any light on this?
How are you submitting the data? I cannot see a submit button.
The code works fine for me in FF using the following (notice I added a submit button):
<form action="" method="post">
<li class="stream-item stream-header search-input-item">
<span class="add-on"><i class="icon-search"></i></span>
<input type="text" class="input-xxlarge" id="appendedInputButton" autocomplete="off" name="q">
<button class="btn btn-primary" type="submit">Search</button>
</li>
</div>
<p></p>
<label class="radio">
<input type="radio" name="opt" value="1" checked>
Option 1
</label>
<label class="radio">
<input type="radio" name="opt" value="2">
Option 2
</label>
<input type="submit" name="submit" value="submit" >
</form>
Result from print_r($_POST);
Array ( [q] => [opt] => 1 [submit] => submit )