Erase value if checkbox is checked - php

I would like the following:
Data = "123";
if checkbox clicked {Data="";}
Submit-button
That was the best way I could explain it.
I have tried, but I can't get the checkbox to overwrite the data with an empty value.

<form action="form.php" method="post">
<input type="text" value="" name="data" />
<input type="checkbox" value="checkbox" name="checkbox" />
<input type="submit" value="Submit" name="submit" />
</form>
That is your form, and this is your PHP
<?php
if(isset($_POST['submit']))
{
if(isset($_POST['checkbox']))
{
$_POST['data'] = "";
}
/* the rest of your form */
}
?>

Related

PHP - Store input values in external php array

I would like to store an user input from a textbox into an external php array by clicking a button. Also, there is a dependency from two radio buttons.
This is my current attempt:
At first, I include the external php array:
<?php
include ('../array.php');
?>
The input form:
<form action="" method="post">
<input type="text" name="textfield" id="txt1" value="">
<input type="radio" name="name" id="id" value="Type1">Type1<br>
<input type="radio" name="name" id="id" value="Type2">Type2<br>
<button id="submit" name="send" type="submit">Save</button>
</from>
Insert the value into the array:
<?php
if (isset($_POST['send'])){
if (isset ($_POST['name'])){
if ($_POST['name']=='Type1'){
$newword = $_POST['textfield'];
$array[] = $newword;
}
}
}
?>
But the values don't "stack", meaning that the array isn't growing with each button click.
Can anyone help please? :D
Thanks in advance!
If you want to stack values, you need to store them somewhere to keep values between each requests to server.
Values can be stored in session (for current user only) or in any database supported by PHP.
Here an example with session :
index.php :
<?php
include ('./array.php');
?>
<form action="" method="post">
<input type="text" name="textfield" value="">
<input type="radio" name="name" value="Type1">Type1<br>
<input type="radio" name="name" value="Type2">Type2<br>
<button id="submit" name="send" type="submit">Save</button>
</from>
array.php :
<?php
session_start();
if (!is_array($_SESSION['persistentValues'])) {
$_SESSION['persistentValues'] = array();
}
if (isset($_POST['send']) && isset($_POST['name']) && $_POST['name']=='Type1') {
$_SESSION['persistentValues'][] = $_POST['textfield'];
}
print_r($_SESSION['persistentValues']);
?>
<form action="" method="post">
<input type="text" name="textfield" id="txt1" value="">
<input type="radio" name="name" id="id" value="Type1">Type1<br>
<input type="radio" name="name" id="id" value="Type2">Type2<br>
<button id="submit" name="send" type="submit">Save</button>
</from>
in the file php put this
<?php
if (isset($_POST['send']) && $_POST['textfield'] && $_POST['name']){
if ($_POST['name']=='Type1'){
array = require('./array.php');
$newword = $_POST['textfield'];
$array[] = $newword;
}
}
?>

How to get customized attributes value of a html radio button in php

<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;
}
?>

Two buttons into same form

I'm writing a form and this is its structure:
HTML:
<form method="post" action="script.php">
<input type="text" name="nombre" >
<input type="submit">
<input type="submit">
</form>
PHP:
<?php
if(isset($_POST))
$options = array();
$options[] = "";
// here I use the value input in "name" text input,
// so I need to get it from the form
for ($i=0;$i<=count($buscar)-1;$i++) {
$options[] = "<option value='{$i}'>{$dato}</option>";
}
echo '<select class="" id="articles" size="1" name="articles">';
echo implode("\n", $options);
echo '</select>';
?>
Is there any way to tell the first submit to let the php be executed (because it creates a select item)?
Then, when the select is selected, click on the second submit and send the complete form?
You can do in two ways:
Different Naming:
<input type="submit" name="sub1" />
<input type="submit" name="sub2" />
And you can check it using:
isset($_REQUEST["sub1"])
isset($_REQUEST["sub2"])
Passing Value:
<input type="submit" name="sub" value="Submit Here" />
<input type="submit" name="sub" value="Submit There" />
And you can check it using:
($_REQUEST["sub"] == "Submit Here")
($_REQUEST["sub"] == "Submit There")

More than one html form in a php file

I have a php and html based tool that has a form that, when submitted, outputs the data reformatted using echo commands.
I'd like to add a 2nd form to the same page that will also output using echo.
My issue is, when I submit the 2nd form the first forms output disappears. I'd like to make it so the echo output from the first form does not go away when the 2nd form is submitted so they will both be on the screen at the same time.
Is there a way I can do this?
Only one <form> block in a page can be submitted at a single time. <input> fields defined in one form will not be submitted when the other form is submitted.
e.g.
<form>
<input type="text" name="foo" />
<input type="submit" />
</form>
<form>
<input type="text" name="bar" />
<input type="submit" />
</form>
Clicking on submit will submit either a foo field, OR a bar field. Not both. If you want both fields to be submitted, then you have to either build them into a SINGLE form:
<form>
<input type="text" name="foo" />
<input type="text" name="bar" />
<input type="submit" />
</form>
or use Javascript to copy the data from one form to another.
<form method="post"> <div>Module1</div> <input type="text"
value="module1" name="module_id"> <input type="text" value="title 1"
name="title"> <input type="text" value="some text 1" name="text">
<input type="submit" name="form_1" value="submit"> </form>
<form method="post"> <div >Module2</div> <input type="text"
value="module2" name="module_id"> <input type="text" value="title 2"
name="title"> <input type="text" value="some text 2" name="text">
<input type="submit" name="form_2" value="submit"> </form>
<?php
if(isset($_POST['form_1'])){
echo '<pre>';
print_r($_POST); }
if(isset($_POST['form_2'])){
echo '<pre>';
print_r($_POST); } ?>
Yes,you can do it.
Eg :
// form1 on page a.php
<form method="post" action="a.php" name="form_one" >
<input type="text" name="form_1" value="if(isset($_POST['form_1'])) echo $_POST['form_1']; ?>" >
<input type="submit" name="submit_1" >
</form>
<?php
if(isset($_POST['submit']))
{
?>
<form method="post" action="a.php" name="form_two" >
<input type="text" name="form_2" value="if(isset($_POST['form_2'])) echo $_POST['form_2']; ?>" >
<input type="submit" name="submit_2" >
</form>
<?php
}
?>
Now when you will submit form_one you will see form_two appear and the value in form one will stay intact in form_one and one the submitting form two the value will remain.
Hope it helped :)

cannot retrieve input field value using PHP $_POST array

I cannot get the $_POST['value'] after form submission.
I have used javascript to assign a value to an input field.
code:
function updateValue(pid, value){
// this gets called from the popup window and updates the field with a new value
document.getElementById(pid).value = value;
}
The above function is called by a popup widow to assign a value to my input field: pid
Here is the form script:
<form action="test.php" method="post">
<input type="text" name="project_name" id="pid" disabled="disabled" />
<input type="submit" id="search" name="search" value="Search" />
</form>
In my test.php, I have done:
include 'functions.php'; //Has the connection script
if (isset($_POST['search'])){
echo $project_id = $_POST['project_name'];
$sql = mysql_query("SELECT * from item
where category like '$project_id %'
");
echo $num = mysql_num_rows($sql);
}
But I am getting the error: Undefined index: project_name
Disabled inputs doesnt post. Use hidden input like this:
<form action="test.php" method="post">
<input type="text" name="project" disabled="disabled" />
<input type="hidden" name="project_name" id="pid" />
<input type="submit" id="search" name="search" value="Search" />
</form>
Try filling the hidden field when you set the value into disabled field
<form action="test.php" method="post">
<input type="text" name="project" id="pid" onchange="document.getElementById("pid-new").value=document.getElementById("pid").value" disabled="disabled" />
<input type="hidden" id="pid-new" name="project_name" />
<input type="submit" id="search" name="search" value="Search" />
</form>
your project_name input field is disabled.
remove this attribute then it will work.
It's because disabled attributes aren't passed along the form requests.
If you're setting the value through javascript, and that part works, replace your text input with this:
<input type="hidden" name="project_name" id="pid" />
this will work

Categories