How to change input value dynamically - php

I want to change an input value depending on a $_POST variable. Something like this:
<?php if ($_POST['yourname'] != "")
{
$('input[name=yourname]').attr('title', $_POST['yourname']);
} ?>
But it doesn't work. How can I do that?
Thanks!
EDIT: I have this input:
<input class="clearme" name="yourname" type="text" title="Insert your name" />

Add directly to html:
<input class="clearme" name="yourname" type="text" title="Insert your name" value="<?php echo isset($_POST['yourname']) ? $_POST['yourname'] : ''; ?>" />

You can't use javascript in PHP. PHP runs on the server, while javascript runs in the clients browser.
You need to do this in pure javascript, not PHP.

Try this in the one file, replacing 'name_of_cur_file.php' with the name of the current php file.:
<?php
if($_POST['yourname'] > ""){
$yourname = $_POST['yourname'];
}else{
$yourname = ""; // set to avoid errors
}
?>
<form action="name_of_cur_file.php" method="post">
<input type="text" value="<?php echo $yourname;?>" />
<input type="submit" value="go" />
</form>
WARNING!!! - Lacking any validation for this example!!!

If you want the code sent from the server to execute then:
<?php if ($_POST['yourname'] != "")
{
<script type="text/javascript">
$(document).ready(function () {
$('input[name=yourname]').attr('title', $_POST['yourname']);
});
</script>
} ?>

You have jquery code in php.You have complete first php tag then you have write your jquery code in script code like
<?php if ($_POST['yourname'] != "")
{
?>
<script>
$('input[name=yourname]').attr('title', $_POST['yourname']);
</script>
<?php
} ?>
You can also write php code only like
<?php
$title="Insert your name";
if ($_POST['yourname'] != "")
{
$title=$_POST['yourname'];
} ?>
<input class="clearme" name="yourname" type="text" title="<?php echo $title; ?>" />

Related

How to add PHP code inside input's value in a form [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 4 years ago.
I want make some php test in my form and i've followed a tutorial but i don't get the result i wanted. I want to make sure that the borne's value isn't null. Then send the value to another page "exo.php" to use it.
The problem is when i add the php code inside input's value for example it's not cosindring it as php code but as a string so it prints the php code.
this is my code :
<?php
if (isset($_GET["submit"])) {
$borneErr="";
$borne="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["borne"])) {
$borneErr = "Missing";
}
else {
$borne = $_POST["borne"];
}
}
}
?>
<form class="" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<label> Borne : <input type="text" name="borne" value="<?php echo htmlspecialchars($borne);?>">
<span class="error">* <?php echo $borneErr;?></span> </label>
<button type="submit" value="OK"> Send !</button>
</form>
this is the result i get in this image below :
Your code raises notice
Undefined variable: borne
on this line:
<label> Borne : <input type="text" name="borne" value="<?php echo htmlspecialchars($borne);?>">
And also this notice
Undefined variable: borneErr
on this line:
<span class="error">* <?php echo $borneErr;?></span> </label>
You can fix that by defining the variable outside of the condition.
The form has a method="POST" attribute.
But you're checking the condition against GET data:
if (isset($_GET["submit"])) {
Also, you're checking existence of a field submit that is not included in the form data, since it's a <button>. You can either change it to <input> or change your PHP condition to check the borne field.
<input type="submit" value="Send !">
or
if (isset($_POST["borne"])) {
The check against $_SERVER["REQUEST_METHOD"] is now redundant so you can get rid of it.
The code could be simplified and polished even more but I'll leave it so it's easier see those errors fixed.
Working code:
<?php
$borneErr = "";
$borne = "";
if (isset($_POST["borne"])) {
if (empty($_POST['borne'])) {
$borneErr = "Missing";
} else {
$borne = htmlspecialchars($_POST['borne']);
}
}
?>
<form class="" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<label> Borne : <input type="text" name="borne" value="<?php echo $borne; ?>">
<span class="error">* <?php echo $borneErr;?></span> </label>
<input type="submit" value="Send !">
</form>
just add value inside quotation mark "HERE"
<input type="text" name="borne" value="<?php echo htmlspecialchars($borne);?>">
You can simply choose one here
<input type="text" name="borne" value="<?=$_SERVER["PHP_SELF"]?>"><Br>
OR
<input type="text" name="borne" value=""> /* leave it blank cause if blank i will submit automatically on the same page. */

If statement with html id

I have the code below that gets an id if there is any and asks you to click a button to show you data.
<input type="text" id="date" value="<?php echo $_GET['id']?>" class="css-input" placeholder="Posting Date..." readonly="readonly" /><br /><br />
<input type="button" id="validate" value="Let's get to work!" class="btn" />
The code below takes you to the data automatically without needing to click a button if there is an id there.:
jQuery(document).ready(function() {
jQuery("#validate").click();
});
How can I do an if statement that if #validate is not null then execute the jQuery code else don't. How could I do that whenever I do <?php if(#date != null) I get an error, any ideas?
<?php
if($_GET['id'] != NULL){
?>
<script>
jQuery(document).ready(function()
{
jQuery("#validate").click();
});
</script>
<?php }?>
</head>

How to save session data in to text box value?

I have some data in a session and I want to save it in text box value, using PHP. But the problem is when saving, just first token of string will be saved, like below example:
<?php session_start();?>
<html >
<head>
</head>
<body>
<form >
<?php echo $_SESSION['institute']="rebaz salih" ?>
<input type="text" <?php echo "value=".$_SESSION['institute']; ?> required />
</form>
</body>
Output will be:
rebaz salih
rebaz
Couple of things:
<?php echo $_SESSION['institute']="rebaz salih" ?>
Is this supposed to be the assignment? or just a print line? in any case, try getting rid of the echo.
Both #edCoder, and #chethan194 are on the right track... the value belongs on the outside, but you really should be using a new variable for institute. For example:
//htmlspecialchars will clear out any quotes, etc so it will work in the browser.
<?php
$value = htmlspecialchars($_SESSION['institute'], ENT_QUOTES);
?>
<form >
<input type="text" value = "<?php print $value; ?>" required />
</form>
Here is the full example:
<?php
php session_start();
//i don't know where you get the institute, so i will leave it here for now...
$_SESSION['institute']="rebaz salih";
$value = '';
if (!empty($_SESSION['institute']))
{
$value = htmlspecialchars($_SESSION['institute'], ENT_QUOTES);
}
?>
<html>
<head>
</head>
<body>
<form>
<input type="text" value = "<?php print $value; ?>" required />
</form>
</body>
You can put the value attribute outside php
<?php echo $_SESSION['institute']="rebaz salih" ?>
Wrong - <input type="text" <?php echo "value=".$_SESSION['institute']; ?> />
Right - <input type="text" value = "<?php echo $_SESSION['institute'];?>" />
</form>
<input type="text" value="<?php echo $_SESSION['institute']; ?>" required/>

Hide a form field with a specific id after submit

I'm new here and a super noob in programming. I'm having trouble with my project. My problem is that I'd like hide the form after submit and retain the data input in it.
Here's my code:
<?php
$displayform = true;
if (isset($_POST['trck']))
{
$track = addslashes(strip_tags($_POST['tracknumber']));
$ord = $_POST['id'];
$displayform = false;
if (!$track)
echo "Please enter your tracking number!";
else
{
mysql_query("update `orderdetails` set `trackno`='$track' where `id`='$ord'");
}
if ($row2['id']==$ord)
echo $_POST['tracknumber'];
}
if ($displayform)
{
?>
<form method="post" action="">
<input type="text" name="tracknumber" id="tracknumber" size="30" maxlength="30" placeholder="Enter your track number here." />
<input type="hidden" name="id" value="<?php echo $row2['id']; ?>">
<input type="submit" name="trck" id="trck" value="Save" onclick="return confirm(\'Are you sure you want to save this tracking number?\');" />
</form>
</td>
</tr>
<?php
}
}
?>
This code was inside a while loop and my problem with this is that after I submit all the form is hidden. All I want is to hide the form with the specific ID on a query.
Simplest way is to use jQuery hide
Include the jquery as
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
$("#buttonid").click(function(){
$("#formid").hide();
});
You are looking for something like this in your <form> tag:
<form method="post" action="" id="awesome_form" onSubmit="document.getElementById('awesome_form').style.display = 'none';">
use the jQuery and use :
$("#submitButtonId").click(function(){
$('#formId').hide();
});
or else in pure javascript use
<input type="submit" value="submit" onClick="hideIt()"/>
<script type="text/javascript" >
function hideIt() {
document.getElementById('formId').style.display = 'none';
}
</script>
its better not to use inline javascript

PHP keep form info after submit form failed

Hello
I am building a form in a mvc system view, and i want that all the inserted values will be kept,in case of form submit failure.
How can this be done: i tried like (example for a field):
<label for="user_firstname">Nume</label>
<input id="user_firstname" type="text" name="user_firstname" value=<?= $_POST['user_firstmane'] ?> >
<? if (isset($errors['user_firstname'])): ?>
<span class="error"><?= $errors['user_firstname']; ?></span>
<? endif; ?>
but of course, it doesn't work the first time (when no post action is done).
what is the simplest way to do this? any ideas?
thank you
Just loop through the DOM in javascript and put the PHP $_POST data into the input.value
<script type='text/javascript'>
<?php
echo "var jsArray = new Array();";
foreach ($_POST as $key=>$value){
echo "jsArray['$key'] = '$value';"; //turn it into a javascript array
}
?>
// Grab all elements that have tagname input
var inputArr = document.getElementsByTagName("input");
// Loop through those elements and fill in data
for (var i = 0; i < inputArr.length; i++){
inputArr[i].value = jsArray[inputArr[i].name];
}
</script>
I would suggest something like:
<label for="user_firstname">Nume</label>
<input id="user_firstname" type="text" name="user_firstname" value=<?(isset($_POST['user_firstname']) ? $_POST['user_firstname'] : ""; ?>>
<? if (isset($errors['user_firstname'])): ?>
<span class="error"><?= $errors['user_firstname']; ?></span>
<? endif; ?>
You also had a typo in the $_POST["user_firstmane"] should be $_POST["user_firstname"] :)
value="<?php echo isset($_POST['user_firstname'])? $_POST['user_firstname'] : "" ?>"
You mean you want to keep the value of the form when it failed to submit? You can use $_SESSION to store the value in the check page. For example:
check.php
<?php
session_start();
if (strlen($_POST['user_firstname']) < 5) { //for example
$_SESSION['user_firstname'] = $_POST['user_firstname'];
}
?>
In your current form. change value=<?= $_POST['user_firstmane'] ?> to value="<?=$_SESSION['user_firstname']?>", so:
<label for="user_firstname">Nume</label>
<input id="user_firstname" type="text" name="user_firstname" value="<?=$_SESSION['user_firstname']?>" />
<? if (isset($errors['user_firstname'])): ?>
<span class="error"><?= $errors['user_firstname']; ?></span>
<? endif; ?>
<input id="FirstName" name="FirstName" placeholder="First name" title="First Name" required="" tabindex="1" type="text" value="<?php if(isset($_POST['FirstName'])){ echo htmlentities($_POST['FirstName']);}?>"/>
This code is much more easier to keep form info after submit form failed.

Categories