Is there a possibility to add the value tag to an input with javascript?
e.g. to change a input fild in a form from:
<input type="text" name="mail" id="mail">
to:
<input type="text" name="mail" id="mail" value="<?php echo $_SESSION['email'] ?>
EDIT:
<?php
echo '
<script type="text/javascript">
document.getElementById("mail").setAttribute("value", "<?php echo $_SESSION["email"];?>");
</script>';
?>
Use setAttribute() to fill in the attribute of an element.
document.getElementById("mail").setAttribute("value", "<?php echo $_SESSION['email'];?>");
<input type="text" name="mail" id="mail">
You can't use <?php inside a PHP string. Use concatenation instead.
<?php
echo '
<script type="text/javascript">
document.getElementById("mail").setAttribute("value", "' . $_SESSION["email"] . '");
</script>';
?>
Yes, you can access the value field of an input with javascript
document.getElementById('mail').value = "someemail#email.com";
Not sure what you are trying to achieve but you could save PHP value as a JS variable and go from there. Something like this?
var email = '<?php echo $_SESSION['email'];?>';
document.getElementById('mail').value = email;
Using the method echo, returning the element input.
$youvalue = "This is the value of the input"
echo '<input type="text" name="mail" id="mail" value="'.$yourvalue.'">';
Using the method echo again, returning a script.
$youvalue = "This is the value of the input"
echo '<script>var x = document.createElement("input");x.type = "text";x.name = "mail";x.id = "mail";x.value = "'.$youvalue.'"; document.body.appendChild(x);</script>'
Related
I am echoing in the screen the (id,name,city) from MySQL.
But the problem and the challenge is inserting the values from MySQl into some inputs.
I am using the code:
verificar_id($pdo);
function verificar_id($pdo){
if(isset($_POST['verificar']) && isset($_POST['id_cliente'])){
session_start();
$id_cliente = $_POST['id_cliente'];
$sql= $pdo->prepare("SELECT * FROM `clientes` where `id`=?");
$sql->execute(array($id_cliente));
$info = $sql->fetchAll(PDO::FETCH_ASSOC);
echo "<pre>";
print_r($info);
echo "</pre>";
foreach ($info as $key => $value) {
$_SESSION['id'] = $value['id'];
$_SESSION['nome'] = $value['nome'];
$_SESSION['cidade'] = $value['cidade'];
}
}
}
?>
<script type='text/javascript'>
var nome = document.getElementById('id').value = "<?php echo $_SESSION['id'] ?>"
var nome = document.getElementById('nome').value = "<?php echo $_SESSION['nome'];?>"
var nome = document.getElementById('cidade').value = "<?php echo $_SESSION['cidade'];?
>"
</script>
And My HTML:
<body>
<form method="post" id="form">
<input type="number" name="id_cliente">
<input type="submit" name="verificar">
</form>
<input type="text" name="id" id="id">
<input type="text" name="nome" id="nome">
<input type="text" name="sobrenome" id="cidade">
</body>
And this is the result:
How you can see the result is almost exactly what i wanted: The only problem in all this is the browser is not doing what should do. Of some how, the browser is understanding that this a Js code(and inside the code, it is appearing the same result tha the "print_r", but is not running and i don't know how to solve this.
Someone can help me to solve this problem? Or solve this or show another way to me get the result? Pls.
Maybe another way to insert the values into a input.
How can I get the values in the while loop using button with js?
<?php
while ($row = mysqli_fetch_array($query))
{
echo '<input type="text" name="sample" value="'.$row['sample'].'">';
}
?>
Thanks
You can get the value of textbox when you click on a button.
<input type="text" name="sample[]" value="abc" class="valueInput">
<input type="text" name="sample[]" value="xyz" class="valueInput">
<input type="text" name="sample[]" value="pqr" class="valueInput">
<input type="button" class="getValue" value="Get Value">
Note: I have set the static input box you can make it dynamic but make sure please add the class as valueInput.
Jquery Code.
$(document).on('click','.getValue',function(){
var valArr = [];
$(".valueInput").each(function(){
valArr.push($(this).val());
});
console.log(valArr);
})
The name of the input field should indicate an array with [].
<form action="action.php" method="POST">
<?php
while ($row = mysqli_fetch_array($query)){
echo '<input type="text" name="sample[]" value="'.$row['sample'].'">';
}
?><input type="submit">
</form>
action.php: (Processes the data on submit)
<?php
echo "<pre>";
print_r($_POST['sample']);
echo "</pre>";
I have built a contact form for my wordpress site. Four fields are there - name, email, subject, message. For logged in users I want their name and email to be auto-filled in their respective fields in the form and those name, email fields will be disabled for them to edit. And for non-logged in users they will put name, email fields manually. I have put this code in the page template file -
<?php
$current_user = wp_get_current_user();
$user_email = $current_user->user_email;
$user_name = $current_user->user_firstname.' '.$current_user>user_lastname;
if ( 0 != $current_user->ID ) {
echo '<script type="text/javascript">
document.getElementById("curUserName").value = "'.$user_name.'";
document.getElementById("curUserName").disabled = "disabled";
document.getElementById("curUserEmail").value = "'.$user_email.'";
document.getElementById("curUserEmail").disabled = "disabled";
</script>';
}
?>
But this code is disabling name, email fields for both users (logged in and non-logged in). I have controlled the script through if condition. Still the javascript is applying for both users. Please advise where I have gone wrong.
Here is the form html -
<form action="/success.php" method="post">
<label>Name :</label><br>
<input type="text" id="curUserName" name="sender_name" required><br>
<label>Email :</label><br>
<input type="text" id="curUserEmail" name="sender_email" required><br>
<label>Subject :</label><br>
<input type="text" name="sender_sub"><br>
<label>Message</label><br>
<textarea name="sender_msg" rows="4" cols="60" required></textarea><br>
<input type="submit" name="submit" value="Send">
</form>
The source you are going to change has disabled option already.. so just remove it if the user is not logged in :)
<?php
$current_user = wp_get_current_user();
$user_email = $current_user->user_email;
$user_name = $current_user->user_firstname.' '.$current_user>user_lastname;
echo '<script type="text/javascript">';
if ( 0 != $current_user->ID )
{
echo 'document.getElementById("curUserName").value = "'.$user_name.'"
document.getElementById("curUserName").disabled = "disabled"
document.getElementById("curUserEmail").value = "'.$user_email.'"
document.getElementById("curUserEmail").disabled = "disabled"';
}
else
{
echo 'document.getElementById("curUserName").disabled = false;
document.getElementById("curUserEmail").disabled = false;';
}
echo '</script>';
?>
Don't echo javascript with php. It's bad practice.
Try using value tags in your inputs, check if user logged in with a ternary operator, and if so, echo to value tag their credentials.
<?php (is_user_logged_in()) ? $current_user = wp_get_current_user() : $current_user = false; ?>
<label>Name</label>
<input type="text" id="curUserName" name="sender_name" value="<?php ($current_user) ? echo $current_user->user_name : '' ?>" required>
<label>Email</label>
<input type="text" id="curUserEmail" name="sender_email" value="<?php ($current_user) ? echo $current_user->user_email : '' ?>" required>
I have only used software like Adobe Muse in the past so I am quite new to coding, but I think I have the basics down.
I am trying to create a form that will alter the content on a template site that will be duplicated.
I created the form:
<form class="pure-form" id="Builder" method="POST" action="scripts\build.php">
<fieldset class="pure-group">
<input type="text" class="pure-input-1" placeholder="Store Name" name="Name">
<textarea class="pure-input-1" placeholder="Description" name="Description"></textarea>
<textarea class="pure-input-1" placeholder="About Paragraph" name="About"></textarea>
<input type="text" class="pure-input-1" placeholder="Store Address" name="Address">
<input type="text" class="pure-input-1" placeholder="Contact Number" name="Number">
<input type="text" class="pure-input-1" placeholder="Email Address" name="Email">
<input type="text" class="pure-input-1" placeholder="Tumblr Username" name="username">
<input type="text" class="pure-input-1" placeholder="Unique ID" name="unique">
<button name="btnbuild" type="submit" class="pure-button pure-input-1 pure-button-primary">Create Website</button>
</fieldset>
The form seems to be functioning alright.
I created a PHP file to get the data from this form:
<?php
$name = $_POST["Name"];
$descrip = $_POST["Description"];
$about = $_POST["About"];
$address = $_POST["Address"];
$number = $_POST["Number"];
$email = $_POST["Email"];
$username = $_POST["Username"];
$unique = $_POST["Unique"];
?>
I am not sure about the code for the images, but the variables should work alright.
In my index.html I used the following method to find and replace the strings that need to be replaced on the page.
<head>
<script>
window.onload = function() {
<?php include 'build.php'; ?>;
document.body.innerHTML = document.body.innerHTML.replace("EHNAME", "<?php echo $name; ?>");
document.body.innerHTML = document.body.innerHTML.replace("EHDESCRIPTION", "<?php echo $descrip; ?>");
document.body.innerHTML = document.body.innerHTML.replace("EHABOUT", "<?php echo $about; ?>");
document.body.innerHTML = document.body.innerHTML.replace("EHADDRESS", "<?php echo $address; ?>");
document.body.innerHTML = document.body.innerHTML.replace("EHNUMBER", "<?php echo $number; ?>");
document.body.innerHTML = document.body.innerHTML.replace("EHEMAIL", "<?php echo $email; ?>");
document.body.innerHTML = document.body.innerHTML.replace("EHUNIQUEID", "<?php echo $unique; ?>");
document.body.innerHTML = document.body.innerHTML.replace("EHUSERNAME", "<?php echo $username; ?>");
document.title = name; }
</script>
</head>
The problem is that it doesn't update the strings at all.
Any help to figure this out would be appreciated.
If I got it right, your initial form post the data to: scripts\build.php and later you are trying to get this data in another page index.html, this seems to be to problem.
Even if you import the build.php in the index.html file, this is another request, so the posted data to the build is already gone. You can solve this problem making the post directly to the index or persisting the data (in the session, for example, or database) and retrieving it in the index.html file.
I want to know why the variable user won't work.
<script type="text/javascript">
var user = <?php echo $r;?>;
document.write(user);
</script>
<form method="post" action="<?PHP echo $_SERVER["PHP_SELF"]?>">
<input type="text" name="username" placeholder="username"><br><br>
<input type="password" name="pw" placeholder="password"><br>
<input type="submit" value="UPDATE" ="loadXMLDoc()">
</form>
Assuming $r is a string, you need to wrap it in quotes:
var user = '<?php echo $r;?>';
Without the quotes, the browser is seeing:
var user = Sandeep;
Better still, use PHP to JSON encode the string, which will escape any quotes and prevent an XSS vulnerability:
var user = <?php echo json_encode($r);?>;
Side note, echoing $_SERVER["PHP_SELF"] into HTML is known XSS vulnerability. You should run it through htmlspecialchars():
<form method="post" action="<?PHP echo htmlspecialchars($_SERVER["PHP_SELF"])?>">