Javascript, PHP, Saving field value - php

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.

Related

Getting input value and put it in a variable PHP

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!

search form prevent remove other query

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>

Radio buttons updating a database deletes the entry?

so I'm creating a small form where you can update the values of some fields pulled from a database.
Two of the fields I have however are acting wrong, when the page loads they display the information correctly but when you press 'submit' the fields in the MySQL table go blank. All the other fields update correctly including a checkbox, but not these two radio buttons.
Here's the code for it, I'm sorry to be asking this but does anyone see the error? I'm rather new with PHP so I'm not sure what to look for.
Snippet of the PHP Code (I can post the full code of it, but it's long):
<?php
if(isset($_POST['submit'])) {
$mbr=$_POST['mbr'];
$rec=$_POST['rec'];
$update = $dbconnect->query("UPDATE testing SET mbr='$mbr', rec='$rec'");
}
?>
HTML Code for the 2 radio buttons:
<input type="radio" name="mbr" <?php echo $upChecked; ?>>Up <input type="radio" name="mbr" <?php echo $downChecked; ?>>Down
<input type="radio" name="rec" <?php echo $yesChecked; ?>>Yes <input type="radio" name="rec" <?php echo $noChecked; ?>>No
Nevermind I'm dumb and tired and missed the obvious solution, I forgot to put the values of the buttons in their code.
How they should be
<input type="radio" name="mbr" value="up" <?php echo $upChecked; ?>>Up <input type="radio" name="mbr" value="down" <?php echo $downChecked; ?>>Down
<input type="radio" name="rec" value="yes" <?php echo $yesChecked; ?>>Yes <input type="radio" name="rec" value="no" <?php echo $noChecked; ?>>

PHP - Codeigniter radio buttons

Im using Codeigniter - I am displaying radio buttons like so :
<fieldset>
<legend>Part Time / Full Time:</legend><br>
<input type="radio" name="time" id="radfull" value="fulltime"> Full Time<br/>
<input type="radio" name="time" id="radpart" value="parttime"> Part Time
</fieldset>
Which works fine, they display.
Here is the code in the model.
'time'=>$this->input->post('time'),
which also works fine , it saves the choice to the database. But how do I get the radio button to populate when the page loads with the choice from the database?
You can use the form helper, which has a function called "set_radio", the very last function on this page;
https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html
<input type="radio" name="time" value="fulltime" <?php echo set_radio('time', 'fulltime'); ?> />
<input type="radio" name="time" value="parttime" <?php echo set_radio('time', 'parttime'); ?> />
I hope this helps.
Just as #Craig said, using set_radio is what you will need.
php
<input type="radio" name="myradio" value="2" <?php echo set_radio('myradio', '2'); ?> />
Note
The value of this radio button is "2". Whatever the value is, needs to be the second parameter in set_radio.
Your code would look something like this:
HTML
<fieldset>
<legend>Part Time / Full Time:</legend><br>
<input type="radio" name="time" id="radfull" value="fulltime" <?php echo set_radio('time', 'fulltime'); ?>> Full Time<br/>
<input type="radio" name="time" id="radpart" value="parttime" <?php echo set_radio('time', 'parttime'); ?>> Part Time
</fieldset>
If you want either option checked by default (when the page loads) that is where you would add the third parameter TRUE to the corresponding radio.
ALSO NOTE
In your controller, you will need to load the form_validation library for any of this to work.
php
$this->load->library('form_validation');
Hope this helps!
EDIT
My apologies, I misread the question. What I had above is...before the form is officially/successfully submitted. In your case, the form has already been (successfully) submitted and you want to...edit the form (for lack of better words) - so you need a way of populating the inputs with whatever the user chose.
What I have done in the past - in my view - is just have a ternary operation right on the element.
For example, let's say I am returning a user, and there is a "time" property.
HTML
<fieldset>
<legend>Part Time / Full Time:</legend><br>
<input type="radio" name="time" value="fulltime" <? echo($user['time'] === 'fulltime') ? selected="selected" : ''; ?> />
<input type="radio" name="time" value="parttime" <? echo($user['time'] === 'parttime') ? selected="selected" : ''; ?> />
</fieldset>
Maybe that's closer to what you are looking for?

Php checkbox using the get method

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>

Categories