I've got this :
<form action="index.php" method="get">
<input type="checkbox" name="Convs_revenue"
<? echo (isset($_GET['extra_Data'])?"value='yes'":"value='no'");?>
<? if (isset($_POST['extra_Data']) ) echo 'checked="checked"'; ?> >extra_Data </input>
<input type="submit" value="send">
</form>
Now I need to keep the value of tp keep the value of the checkbox, and to unset it when its unchecked. I MUST use the GET method for this form, and this need to be at the same page as well.
What happen is that its always seem to be checked no matter what I do, and the get array always keep this at on...
Well for a start, <input /> needs no closing tag. I've also tidied up the code a little so it's a bit more readable.
I've added an extra check to make sure $_POST['extra_Data'] isn't empty. It will show up as set if it is posted empty somehow, I can't see how you're generating the POST itself.
<form action="index.php" method="get">
<input name="Convs_revenue" type="checkbox" value="<?php (isset($_GET['extra_data']) ? 'yes':'no'); ?>" <?php (isset($_POST['extra_Data']) && !empty($_POST['extra_Data'] ? 'checked="checked"' : '') ?> />
<input type="submit" value="send">
</form>
Related
if i put this : NO WORKS
<?php
if($_POST['send']=="ok")
{
print $_POST['opt']['nombre']
}
?>
<form action="" method="post" style="margin:0px;">
<input type="text" name="opt['nombre']" value="Hello" />
<input type="hidden" name="send" value="ok">
</form>
If i Put this : WORKS
<?php
if($_POST['send']=="ok")
{
print $_POST['opt']['nombre']
}
?>
<form action="" method="post" style="margin:0px;">
<input type="text" name="opt[nombre]" value="Hello" />
<input type="hidden" name="send" value="ok">
</form>
Why Happend this , the only change it´s in input file this opt['nombre'] by opt[nombre]
I don´t understand why happend this, it´s possible fix this problem because i want put opt['nombre'], and i think it´s the right
When works the result i get it´s "Hello" but only change this symbol inside tags as [''] by []
DIFFERENCES WORKS AND NO WORKS WHEN SEND POST FORM :
SEND FORM AND DON´T GET HELLO
<input type="text" name="opt['nombre']" value="Hello" />
SEND FORM AND GET HELLO
<input type="text" name="opt[nombre]" value="Hello" />
DIFFERENCE PUT INSIDE [] QUOTES AS 2 OR DON´T PUT, THANK´S
var_dump($_POST) and take a look at the keys. You'll see the difference. The single quotes in the HTML name attribute become part of the string key in the PHP array when you do it the first way. You'd have to access it with the single quotes as part of the string to get it.
print $_POST['opt']["'nombre'"];
Or better yet, just do it the second way, so your PHP code won't have to use a silly key like that. :-)
I have a problem of getting the value of an input element. Thanks in advance for help! This is the sample code:
<form action="#" method="POST">
<input name="X" value="3" />
<?php
//question: how can I put the value of input in a variable
//$_POST['X']; isn't applicable since I am in the same form.
?>
</form>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<input name="X" value="3" />
<input type="submit" value="Submit">
</form>
<?php
if(isset($_POST['X']) == true){
echo $_POST['X'];
}
?>
First of all submitting a form on its own page is a bad practice because whenever you reload the page the form will submit everytime.
Best approach is that you set the action attribute of form to another file or link.
fileOne.php (where your form is located):
<form action="anotherfile.php" method="POST">
<input name="X" value="3" />
<button type="submit" name="submitForm">Submit</button>
</form>
Then in anotherfile.php you will do this:
$check = $_POST["submitForm"];
If(isset($check))
{
$myInputValue = $_POST["X"];
header("Location: fileOne.php/?val".$myInputValue);
}
Then in fileOne.php you can get the variable and its value which is sent via link in the browser like this:
$getInputValue = $_GET["val"];
Now you can echo out the $getInputValue variable wherever you want.
If the the information is not sensitive this is the best approach you got. But if it is, try saving that value in the session and retrieving in the file where you want. I hope it helps. Typed code via app so it might throw an error. But i think this code will work fine. Good luck!
html:
<form method="GET">
<input type="text" name="k" id="header-search" value="<?=$_GET["k"];?>"/>
<input type="submit" id="header-submit" value="" />
</form>
When current url is:
https://example.com/search/cats?b=5
After click on submit button it remove b query and show like this:
https://example.com/search/cats?k=sometext
But i want this result:
https://example.com/search/cats?b=5&k=sometext
I have other query like b, d and also c maybe add more in future, so this is not a static, maybe url have b maybe d or maybe c or maybe all together or maybe no one.
I tried this but looks like no changes:
action="<?=$_SERVER['REQUEST_URI'];?>"
You can add the variables inside hidden inputs:
<form method="GET">
<?php if(isset($_GET['b']){ ?>
<input type="hidden" name="b" value="<?=$_GET["b"];?>"/>
<?php } ?>
<input type="text" name="k" id="header-search" value="<?php echo isset($_GET["k"]) ? $_GET["k"] : '';?>"/>
<input type="submit" id="header-submit" value="" />
</form>
However this solution will not work very well if you have many different types of variables that may or may not exist all the time. If you add all the variables as hidden, they will all be visible when you submit the form. To prevent this, you will need to check if the variables are isset() and only print them if they are.
Here is a solution that uses hidden fields and handles any amount of get parameters:
<form method="GET">
<?php
foreach($_GET as $key => $value){
// do not make a hidden input for k, there is already a text input for k
if($key != 'k'){
echo '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
}
}
?>
<input type="text" name="k" id="header-search" value="<?php echo isset($_GET["k"]) ? $_GET["k"] : '';?>"/>
<input type="submit" id="header-submit" value="" />
</form>
I am trying to save fields data after submited, Becouse after all the fields are good to go but lets say at the server side the user name is already taken so the form return empty and i dont want that there is the option to do it with PHP like that:
<input value="<?php if(isset($userName)) echo $userName; ?>" />
But the problem is with the radio input, If can some one think about solution about the radio with PHP i will be very thankful, Also i was thinking about Javascript so i will have cleaned code and i was thinking about taking the values from the URL but i am using POST for security reasons.
Summary: If anyone have a solution with PHP or Javascript i will be very thankful, Thank you all and have a nice day.
Try this
<form name="myform" action="" method="post">
<input type="radio" name="language" value="Java" <?php echo(#$_POST['language'] == 'Java'?"checked":""); ?> /> Java
<input type="radio" name="language" value="VB.Net" <?php echo(#$_POST['language'] == 'VB.Net'?"checked":""); ?> /> VB.Net
<input type="radio" name="language" value="PHP" <?php echo(#$_POST['language'] == 'PHP'?"checked":""); ?> /> PHP
<input type="submit" />
I think this may help you.
<input type="radio" value="choice1" name="radio_name" <?php echo(#$_POST['radio_name'] == 'on'?"checked":""); ?> />
If you want to automatically select a radio input you can add the attribute checked to it. What you are going to need will look like this :
<form method="POST">
<?php
// You have some short of list of possible value //
$arrRadioValues = array("value1", "value2", "value3");
// You display them //
for ($i=0; $i<count($arrRadioValues); $i++) {
?>
<input
type="radio"
name="radioInputName"
value="<?php echo $arrRadioValues[$i]; ?>"
<!-- If the value that was posted is the current one we have to add the "checked" so that it gets selected -->
<?php if (isset($_POST['radioInputName']) && $_POST['radioInputName'] == $arrRadioValues[$i]) { echo " checked"; } ?> />
<?php
}
?>
<input type="submit" />
</form>
Adding the checked attribute works a little bit in the same as setting a value to an input. It's just that instead of defining the value attributes, you define the checked attribute when you want that radio to be selected.
I need to pass some data with these 2 methods together ( GET AND POST ).
I write this method, but I don't know if it is safe:
<form method="post" action="profile.php?id=<?php echo $_SESSION['id']; ?>" enctype="multipart/form-data">
<input type="text" size="40" name="title" >
<textarea name="description" rows="2" cols="30"></textarea>
<input id="starit" name="submit" value="Create" type="submit" />
</form>
<?php
a= $_GET['id'];
b= $_POST['title'];
c= $_POST['description'];
?>
Is this code safe ? Or there are other ways to do that ?
This is not a combined GET and POST request; rather, it's a POST request with query parameters.
What you have written would be the right way. Always make sure that you get the expected fields:
if (isset($_GET['id'], $_POST['title'], $_POST['description']) {
// go ahead
}
Btw, make sure that you escape your output:
<form method="post" action="profile.php?id=<?php echo rawurlencode($_SESSION['id']); ?>">
And if you're not uploading files, you don't need to set the enctype of your <form>.
you can use both and get with REQUEST instead of GET or POST, with the same name of params it will get the "request-order" order GET and then POST by default.
http://php.net/request-order
it is in php.ini
This is better :
<form method="post" action="profile.php?id=<?php echo urlencode($_SESSION['id'])); ?>">
don't write method attribute in your form condition
and add formmethod=" " attribute in input...
for example:
<input type="submit" formmethod="get" name="inputGet" value="updateGet" >
<input type="submit" formmethod="post" name="inputPost" value="updatePost" >