I have an "Update Profile" script that retrieves the details of the logged in user in a form input, so they can see what the current values are. Like this:
<input type="text" name="first_name" id="first_name" value='<?= $row->f_name; ?>' title="enter your first name.">
This works. When users navigate to the page they see their first name appear in that input box. When they go to change their name and enter in a new value the form submits but keeps the original value and not the updated one. I assume that is because I am hard-coding a value to it with the value attribute.
How can I show the user the value in the input so that if they choose to not edit it, it does submit with the original value and not a blank, but if they do choose to add text to the input box the new string is submitted?
If I understand you correctly:
value="<?=set_value('first_name', $first_name)?>"
The first parameter is the posted value, the second parameter is the default overridden value.
You may want to use placeholder attributes.
<input type="text" name="first_name" placeholder="<?= $row->f_name; ?>">
`if($_GET['first_name']==''){
$first_name= $row->f_name;
}else{
$first_name= $_GET['first_name'];
}`
SQL Query :
"UPDATEyour_table_nameSETfirst_name= '$first_name' WHEREblablabla= '$blablalba';"
<input type="text" name="first_name" id="first_name" value='f_name; ?>' title="enter your first name.">
Related
I am trying to pass 2 arrays in a MYSQL table using HTML array, I want to insert both values in the same row at the same time of the loop, of course nested loop isn't going to work, the first input value passed successfully, but the second input inserts wrong & unrelated values. I am sure it's because the for loop logic is incomplete, but I can't seem to adjust properly. any help will be appreciated.
HTML (...html code inside PHP then this, $row['id'] is the value that shall be passed to POST):
<input type="text" id="mytextbox" name="comment[]" placeholder = "Add your comments here" required>
<input type="text" id="mytextbox" list="decision[]" name="decision" placeholder = "Choose your decision" required>
<datalist id="decision[]">';
echo '<option value="'.htmlspecialchars($row['id']).'">'.htmlspecialchars($row['name']).'</option>';
<input type="text" id="mytextbox" name="comment[]" placeholder = "Add your comments here" required>
<input type="text" id="mytextbox" list="decision[]" name="decision" placeholder = "Choose your decision" required>
<datalist id="decision[]">';
echo '<option value="'.htmlspecialchars($row['id']).'">'.htmlspecialchars($row['name']).'</option>';
<input type="text" id="mytextbox" name="comment[]" placeholder = "Add your comments here" required>
<input type="text" id="mytextbox" list="decision[]" name="decision" placeholder = "Choose your decision" required>
<datalist id="decision[]">';
echo '<option value="'.htmlspecialchars($row['id']).'">'.htmlspecialchars($row['name']).'</option>';
PHP (after successful POST of $comment as input(1) & $decision as input(2) and working queries):
for ($i=0;$i<count($comment);$i++){
$query = "INSERT INTO table (otherid,col1,col2) VALUES ('$otherid','$comment[$i]','$decision[$i]')";
$result = $dbc->query($query);
}
You are getting only the last decision, because you did not use square brackets in the field name, as you did with the comments field. name="decision" needs to be name="decision[]". Only then will PHP create an array out of multiple passed parameters of the same name; without square brackets, they simply overwrite each other.
The duplicate IDs are only of client-side importance - selecting from those lists, will likely not populate the correct input field, but it has little to do with what actually gets submitted, if you filled those fields by hand. But you should be able to make thos IDs dynamic, for example by appending the row ID.
<datalist id="decision-123">, with a matching list="decision-123" on the input field.
I have the following code snippet of my fields I have in my form:
<input id="username" type="text" placeholder="E-mail Address" value="" name="username"></input>
This is what I have in my input field. Is there anybody who will tell me how to get input values to the field using a url? e.g https://mysite?username=ken and it will show "ken" in the input field?
In your HTML, add the input field like this:
<input type="text" name="username" value="<?php echo htmlspecialchars($_GET['username']); ?>" />
Basically, the value attribute of the text field needs to be set to:
<?php echo $_GET['username']; ?>
The code right above this is how you would output a get variable in php whether you are putting it in a text field or not.
To access get variables, always use:
$_GET['variable_name'];
Then you can assign it to variables or pass it as a function parameter.
**However, I strongly do not recommend passing sensitive information like usernames and passwords through GET variables. **
First off, users could change the URL hence changing the variable. They could also accidentally share the URL with someone and that could give someone else access to their account. I would recommend that you create a cookie on their machine that is set to a random ID, and then in a MySQL database, associate that ID with a username so that you know the user can't accidentally share their account or change their username through the URL.
You can do it like this, make an isset in your php form input that can catch your ken variable from GET post, never forget the method="get" inside the form tag and if you are planning on submitting on the same page you can use action="<?php echo $_SERVER['PHP_SELF']; ?>" inside your form tag.. hope this helps, here is your code.. ^_^
<form id="form" name="form" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<p>Input</p>
<div>
<input type="text" name="nameoffield" id="nameoffield" value="<?php if(isset($_GET['ken'])){echo $_GET['ken'];} ?>"> <br />
</div>
</fieldset>
<div>
<button type="submit" value="Submit" name="submit">
Submit
</button>
</div>
</form>
The <input> tag and other fields of form must be in a <form>tag.
<form action = "https://mysite" method = "get">
<input id = "username" type = "text" placeholder = "E-mail Address" name = "username" value = "<?php echo $_GET['username']; ?>" />
</form>
In the above code, form tag specifies that the method of submission is 'GET' and the action that will be taken on submission is URL to which your form data will be submitted and processed.
Now assuming that your form is in the same URL to which you are submitting your form, you will get the GET value in the same page (or URL), so in the input text field set the value which is obtained by GET method and use it.
All the GET key-value pairs are stored in an associative array $_GET from which you can access the value of a given key by using that as the index of the array.
e.g. Key is username in this case, so to get the value of the username, $_GET['username'] was used.
I am try to get the value of the input field with a custom attribute I have created using PHP. This is my code:
<form action="uploadform.php" method="post">
<input type="text" name="email" id="email" mynewattribute="myemail">
<input type="submit" name="submit">
</form>
//uploadform.php
<?php
//I know $name = $_POST['email']; will give me the value but I would like to get the value of the input field with "mynewattribute" and not name. Is it possible?
?>
The web browser doesn't know what to do with your custom attribute, so will simply ignore it. The only data sent when you submit the form is the values of "successful" elements. So your custom data will never be sent, and can never be read by the receiving script.
The best place to put such data is into hidden input fields. One possibility is to use names with square brackets in, which PHP automatically converts into arrays. e.g.
<input type="text" name="email[value]">
<input type="hidden" name="email[magic]" value="true">
Populates an array like this:
$_POST['email']['value'] = '';
$_POST['email']['magic'] = 'true';
I have a live search form on the site that does two things. If it gets any results it displays them, and if not, the visitor can send an email.
There are two input type fields
<input type="hidden" name="myField" id="myField" value="" />
Email: <input name="email-index" id="email-index" type="text" /></b>
In the email field, the visitor inputs the email. And in the value of the hidden field, i want the search query to be passed from the query.
The search query results are displayed one div before this form with
<!-- Results -->
<h4 id="results-text"> <b id="search-string"></b></h4>
where search-string is replaced with the query.
I have put this into jquery
var hidden = "search-string";
$('input[name=myField]').val(hidden);
but nothing really happens, i get an empty output.
Thank you for your help!
If you have $_GET['search-query'] parameter, just output it to input field:
<input type="hidden" name="myField" id="myField" value="<?php echo $_GET['search-query']; ?>" />
It is because you do not actually get anything from jquery.
Change your script to:
var hidden = $("#search-string").text();
$('input[name=myField]').val(hidden);
I had my site working as follows:
Input url in Wordpress options, save and it saves.
Now I want it so set a default value in the input (url of the server) and if I change and save it, on page re-load the new value should appear, not the default value.
Before
<label for="site">
<strong>Site:</strong>
<input type="text" name="url" value="<?php echo $this->getOption($options, 'url') ; ?>">
</label><em>
My attempt
I have now set a default value of the input to be that of the server url but obviously on page re-fresh it will display this value, just unsure how to fix this so if I update the input and press save, on page re-fresh it will retain my last input rather than updating it with the default value.
<label for="site">
<strong>Site:</strong>
<input type="text" name="url" value="http://<?php echo str_replace('www.','', $_SERVER['SERVER_NAME']); ?>/"/>
</label><em>
My suggestion is to set a variable to the original $_SERVER value.
If a value is posted from the form, set the variable to that value instead.
Then, use that variable to populate the input value, rather than the original $_SERVER value.
Something like this:
<?php
// this uses a ternary operator. if ? then : else.
// if value is posted set to that value, otherwise set to $_SERVER value
$url= !empty($_POST['url'])
? $_POST['url']
: 'http://'.str_replace('www.','', $_SERVER['SERVER_NAME']);
// show a message if data is posted (for debugging purposes)
if (!empty($_POST)) {echo "<p>Data was posted.</p>";}
?>
<form action="" method="post">
<label>
<strong>Site:</strong>
<input type="text" name="url" value="<?php echo $url ?>" />
</label>
</form>