Hidden values in php echo - javascript

I have this query in php code:
$sql="SELECT vName,id FROM employee WHERE vName LIKE '%$my_data%' ORDER BY vName";
I have echoed the vName.
echo $row['vName']."\n";
The above values appear in a autocomplete textbox.
In the same aboce echo statement I want to pass 'id' as hiden value. is it possible? I wan to retrieve it in another page.How do I do this?

$sql="SELECT vName,id FROM employee WHERE vName LIKE '%$my_data%' ORDER BY vName";
$hid='<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />';
echo($hid);
echo $row['vName']."\n";
I hope you get the idea.

Echo it out to a hidden input field,
<input type="hidden" name="hidden_id" id="my_hidden_id" value="<?php echo $row['id'];?>"/>
This way you can pass it with a form if you are submitting one or simply add an id to it and get the value to pass to the next page.

I would first make a variable of $row['id'] and when you're still in your form but outside the textbox do this:
<?php echo "<input type='hidden' name='id' value='".$yourvariable."'/>";?>
If it must be completely hidden i recommend using php-sessions.

I guess you want to do that if you want to retrieve the id in other page , you better do this :
<?php echo $row['vname'] ?>

Related

Use array to post html form with every aray value ony by one

I am wondering if the folowing is possible.
I fetch a list of names as an array from a SQL database. I need all those names to be posted with a html form one by one. This action should be activated with one button. When the button is clicked the names should be posted one by one untill all names are posted, then stop. So probebly jquery or javascript is needed but that is new for me. I have been searching but I can not find anything that can help me accomplisch this.
I am sorry for asking this question and my language (english is not my main language) but I don't know if this is even possible and I cant find any corresponding topics while researching..
PS: I can not use Ajax for the post !!
Example to get the names:
$stmt = $mysqli->prepare("SELECT username FROM example WHERE examplefield = 1");
$stmt->execute();
$result = $stmt->get_result(); //only works when nd_mysli is set on the server!
while ($rowid = $result->fetch_assoc())
{
$arrayusername[] = $rowid['username'];
}
I need all the names from the $arrayusername[] to be posted with below form one by one by pressing the following button
<input type="button" value="Post all names one by one"
onClick="sendallvalues('???') "class="example_c" />
// The button should do the following post name 1, end. post name 2 end, post name 3 end. stop script when all names are posted.
<form method="post" target="_example" action="https://www.example.nl">
<input type="hidden" value="<?= $arrayusername[] ?>" name="postvalue" >
</form>
// should be hidden to the user and is only ment for the name atrribute to have a place ! All the stuff needs to happen by pressing that one button !
<script>
function sendallvalues(???) {
//I have no Idea where to begin to make this happen.. But it should post the form one by one with one value at the time untill all names are posted.
}
</script>
Maybe you can explain more detailed what you need.
The value attribute should have one name and you create different input fields.
<form method="post" target="_example" action="https://www.example.nl">
<input type="hidden" value="<?= $arrayusername[] ?>" name="postvalue" >
<input class="example_s" type="submit" value="Post name">
</form>
should be
<?php
$arrayusername = array('Name1','Name2');
echo '<form method="post" target="_example" action="https://www.example.nl">';
foreach ($arrayusername as $key => $value) {
echo '<input type="hidden" value="' . $value .'" name="name-' . $key . '" >';
}
echo'<input class="example_s" type="submit" value="Post name">';
echo '</form>'
?>

detecting input in dynamically generated input

My code dynamically generates a series of input fields. Each row of fields belongs to one database record and I use php's dynamic array notation ('[]') to capture the values.
Then at the input field i want to show the price input value that have been multiplied by total number of item that the data is generated from database.
I used javascript that placed inside foreach looping to detect the input and multiply it, but it only works for the first field, at the second field the multiplied value shown on the first field too.
this is the code structure :
<?php foreach ($barang->result_array() as $row): ?>
<tr>
<td><?php echo $row['nmbr']; ?> </td>
<td><?php echo $row['jumlah']?> </td>
<td><input id="pnwrn" type="text" name="pnwrn[]" value="" />
<input type="hidden" name="id_barang[]" value="<?php echo $row['id_barang']?>" />
<input type="hidden" name="jumlah[]" value="<?php echo $row['jumlah']?>" /> </td>
<td><div id="output"><script type="text/javascript">
var harga = <?php echo $row['jumlah']; ?>;
$('input').bind('input propertychange', function () {
$('#output').html($(this).val()*harga);
});
</script>
</div></td>
</tr>
<?php endforeach; ?>
I tried to make the output show the multiply value in each row but have no result yet..can someone tell me where i make my mistake?
thanks ~
There are several issues that others have pointed out in the comments.
The reason why every value on the page is changing is because you are using an id for the output div instead of a class and your jQuery change handler is saying: "get everything with an id of output and put this new html in it." Aside from that, it's not valid to use an id more than once on a page. So, change both the output and pnwrn to classes.
To make it simple to get the corresponding 'harga' value later, you can just store it as data on the input.
<?php foreach ($barang->result_array() as $row): ?>
<tr>
<td><?php echo $row['nmbr']; ?> </td>
<td><?php echo $row['jumlah']?> </td>
<td><input class="pnwrn" type="text" name="pnwrn[]" value="" data-harga="<?php echo $row['jumlah']?>" />
<input type="hidden" name="id_barang[]" value="<?php echo $row['id_barang']?>" />
<input type="hidden" name="jumlah[]" value="<?php echo $row['jumlah']?>" /> </td>
<td><div class="output"></div></td>
</tr>
<?php endforeach; ?>
The second issue is that you are writing a separate handler in every single table row. This is not only inefficient, it's hard to read the output and completely unnecessary. You just want your script once on the page so don't include it in your loop. To get to the corresponding values for your calculation, you can just retrieve the data value stored on the input. Then, you can use DOM traversal to find the corresponding output div. In this case, go up to the nearest ancestor row, then find its descendent element with the output class. Also, unless you are using a really old version of jQuery, use the on method instead of the deprecated bind.
<script type="text/javascript">
$('input').on('change', function () {
var harga = parseInt($(this).data('harga'));
$('this').parents('tr').find('.output').html($(this).val()*harga);
});
</script>

Keep value in input box when the page is refreshed

I want to keep the value from a input box even when the page is refreshed.
I know that i could do that using just an echo, but i'm connecting two different files.
I have one page with the form:
<form method="POST" action="mypage.php">
<input type="hidden" name="value" value="test">
<input type"submit">
</form>
And another one(mypage.php) that reads the form
<input type="text" name="value" value='<?php echo $_POST['value']; ?>' />
The code is working, bue lets imagine that someone refreshed the page and the value desappears.
I want to know how can I keep the value inside the textbox even when someone refreshes the page.
Thanks.
Create a session to preserve the value:
<?php
session_start();
if((isset($_SESSION["preserve"]))&&($_SESSION["preserve"] != "")){
$_SESSION["preserve"] = $_POST['value'];
}else{
$_SESSION["preserve"] = "";
}
?>
<input type="text" name="value" value='<?php echo $_SESSION["preserve"]; ?>' />
<?php
//when you don't need this session anymore do this:
//(that can also be called in another page of your project)
unset($_SESSION["preserve"]);
?>
Hope that's what you were looking for...

How to put a php variable into an HTML form

I have code that queries an SQL database and displays all of the users in a table, that all works fine, from there you are able to click any user and it displays another table that contains all of their information, that also works fine.
The problem I am running into here is on the page that displays the table of the selected users information. On that page I have a button that allows the user information to be edited and update the DB.
Here is the code for the button:
print "<form method='post' action='adminedituser.php'>
<input type='hidden' name='id' value='$id' />
<input type='submit' value='Click here to edit this user' />
</form>";
When I click this it takes me to a page with an input box that needs to display the current username and allow the user to put in a new value and submit.
I'm trying to call a php variable inside of the HTML form I have:
<?php
$id = filter_input(INPUT_POST, "id");
//Make db connection
$conn=mysql_connect("localhost","root","")or die(mysql_error());
//Step 2: select your db to use
mysql_select_db("final",$conn);
//Step 3: Write the sql that we want to run against the database
$result = mysql_query("select id, firstname, lastname, email, phone, username, password, favchar, bio from user where id=$id");
$username = $result["username"];
?>
<form method='post' id='myForm' name='input' action='adduser.php'>
Username: <input type='text' value="<?php echo ($username); ?>" id='uName' name='uName'>
<input type='submit'>
</form>
The problem is that the variable $username is not being displayed as the text box value.
What am I doing wrong?
Firstly, if I don't say it someone else is: Use mysqli instead of mysql. You can still use the procedural style, if you want (although I can say firsthand it is VERY easy to learn the object style), you just need to do a quick brush up on mysqli. Mysql is deprecated, so it is sort of useless to use. You'll have to learn it eventually, so when you're building a new program, it might be easiest.
Moving on to what you are doing wrong, is that you have not actually fetched anything. MySQL will give you an object, and then you need to sort the rows.
So, what you need to do is pull mysql_fetch_array.
You can do so like this:
while($row = mysql_fetch_array($result)) {
$username = $row["username"];
}
The form code shuld be changed to:
<input type='hidden' name='id' value='<?= $id ?>' />
You forgot to "print" the $id value.
and the code of adminedituser.php:
$id = filter_input(INPUT_POST, "id");
Get the "id" field from POST request of your form.

Store a session variable in html/javascript

I'm trying to grab a session variable in my javascript, but have some problems getting it right..
I didn't get that to work, so instead i tried to store it in a hidden variable in HTML first, but i dont know how to store a session variable there..
Can anyone guide me to the right path?
The code (HTML file): ($_SESSION["price1"] is equal to "20.00")
<input type="hidden" id="price" value="$_SESSION["price"]"/>
The code in javascript:
var sessionValue = document.getElementById("price").value;
What to do?
you missed PHP tags <?php and echo, change to:
<input type="hidden" id="price" value="<?php echo $_SESSION["price"]; ?>"/>
What everyone bellow mentioned is perfectly true, inside HTML you can just wrap stuff in <?php echo $_SESSION['price']; ?> and it will work (notice the <?php tags)
However, the right way to do this -- considering you want to pass a PHP variable (in your case a session variable) to javascript is to directly insert it into a javascript variable instead of accessing the DOM. This way, you also have it instantly and don't have to wait for the DOM to be loaded
<script>
var price = '<?php echo $_SESSION["price"]; ?>';
alert('The price is: ' + price);
</script>
This line:
<input type="hidden" id="price" value="$_SESSION["price"]"/>
Should be:
<input type="hidden" id="price" value="<?php echo $_SESSION["price"];?>"/>
You are missing the php tags
change,
<input type="hidden" id="price" value="$_SESSION["price"]"/>
To,
<input type="hidden" id="price" value='<?php echo $_SESSION["price"]; ?>'/>
Grab your session variable in JavaScript using this:
var my_session_price = "<?php echo $_SESSION["price"]; ?>";

Categories