I have textarea in the UI which i display text inside it from db and the user edits it and clicks on update button.
while($row=$stmt->fetch(PDO::FETCH_NUM,PDO::FETCH_ORI_NEXT)){
<textarea id= <?php echo $row5[0]; ?> name='upAncText[]' rows=1 cols=40> <?php echo $row5[1]; ?> </textarea><br/>
}
Now when i click on update i have to read the id and texarea value inorder to insert into DB. could anyone let me know how to do this in php?
Change your textarea to something like this:
// Are you sure you want $row5 and not $row ?
<textarea name="upAncText[<?= $row5[0]; ?>]"></textarea>
Now, when you grab $_POST['upAncText'], it will be an array with keys that are the textarea ID and values that are the user input.
FYI, <?= ?> is shorthand for <?php echo ?>
Related
I am trying to create an html form where i have a dropdown menu populated with data from stations table. I am able to select a station and then i want to use this station name when a button is clicked to display a table with related data. But i don't understand how i can use selected station to create another query when submit button is clicked. Below is my code:
<form method="post">
<label>Choose a station:</label>
<select name="owner">
<?php
$sql = mysqli_query($conn, "SELECT name FROM stations");
while ($row = $sql->fetch_assoc()){
?>
<option value="owner1"><?php echo $row['name']; ?></option>
<?php
}
?>
</select>
<br><br>
<input type="submit" value="Submit">
</form>
i think onclick must be used but i don't understand what would be posted that i can use for new sql query.
When you click submit button browser send form via POST method to the same page and reload it. So you can capture the value selected in the form with this PHP code:
If (isset($_POST['owner'])) {
// mysqli query where the selected value is $_POST['owner']
}
I have been working on my own PHP project. I have hit an obstacle. I am trying to retrieve the results of a database and print out a form for each result set.
I then wish to interact with one particular result either be deleting it or passing it into a function etc..
Heres is my current code :
<?php
while($row = mysqli_fetch_array($result)){
echo '<div id="post">'; ?>
<form action="" method="post">
<?php
echo "<font size=4>".$row['post']."<br>";
echo "posted by : ".$row['username']."<br>";
$id = $row['p_id']; ?>
<input type="submit" name="choice" value="Y">
<input type="submit" name="choice" value="N">
</form>
<br>
</div>
}
<?php
if($_POST['choice']=="Y"){
// progress
functionA();
}
else if($_POST['choice']=="N"){
// delete or remove
functionB();
}
?>
So my goal here would be click Y to progress that particular result or N to delete/remove the result.
However currently by clicking either button all results either get deleted or progress. I do know that the id should be used to differentiate between posts but I cant quite seem to get it to work. Once the button is pressed it passes all results to either function.
First of all, I suppose you want to know the record to delete. So, add an input to your form:
<input type="hidden" name="id" value="<?php echo $row['p_id']; ?>">
Then, in your script, call:
if( $_POST['choice'] == "Y" )
{
// progress
functionA( $_POST['id'] );
}
elseif( $_POST['choice']=="N" )
{
// delete or remove
functionB( $_POST['id'] );
}
Additional problem: how you can use your db connection inside the functions? Assuming your mysqli connection is named $conn, call the function(s) in this way:
functionA( $_POST['id'], $conn );
Side note: First process $_POST values, then retrieve db records and print it.
Side note 2: take a look at prepared statement.
Read more about variable scope
Read more about prepared statements
Use two different <input type="radio">s for each item with the id in the value and let the user select which to delete and which to process.
So for an item with id 1 the outputted HTML would look like:
<input type="radio" name="items[1]" value="delete"><input type="radio" name="items[1]" value="process">
The name forces the items to go to _POST as an array with the ids as keys and the selected action as their values.
Example html: https://jsfiddle.net/gu1gkwod/
i got a bit of a problem, as you guys can see in my code, i got a foreach looping through my img folder, showing all the images, well i left out the proper styling of the images, since the problem is the form inside every image.
i need to get the value from the radio input, so i can tell my other script to select all the info from the DB where the ID has the same value as the input.
and i needed a if statement so that if nothing is set i can show something saying you need to select a radio before you can listen to any stations.
but my if statement keeps showing 'error', even tho i click on the buttons
i hope anyone could help me out :)
if(isset($_POST['submitRadio')):
$test = $_POST['radio'];
echo $test;
else:
echo 'error';
endif;
foreach(glob('img/radios/big/*.png') as path):
printf('
<form method="post">
<input class="hidden" type="text" name="radio" value="1">
<input type="submit" name="submitRadio" value="Go!">
</form>
');
endforeach;
after doing what GolezTrol told me to do:
<div class="row text-center">
<?php
$sql = "SELECT * FROM radio";
$stmt = $Db->dbh->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
if(isset($_POST['submitRadio'])):
$test = $_POST['submitRadio'];
echo $test;
else:
echo 'error';
endif;
?>
<form method="post">
<?
foreach($result as $radio):
printf('
<button type="submit"
name="submitRadio"
value="'.$radio['id'].'">
Go!
</button>
');
?>
</form>
I've removed my previous solution. I only just saw that you have no actual radio button but hidden elements, so basically the buttons are the radios.
That changes it a little bit because you actually have the alternative suggestion I proposed earlier: The buttons themselves work as radiobuttons that immediately post the chosen option.
To solve your issue, you can use a button instead of an input for submitting the form. In a button, you can make the caption and the value differ from each other, so you can just put the id (or filename, or whatever you need) in the value of the button and don't need the hidden input at all. The content of the button tag is the caption it gets, so the code can look like this:
if(isset($_POST['selectedFile')):
$test = $_POST['selectedFile'];
echo $test;
else:
echo 'error';
endif;
?><form method="post"><? // Open the form outside of the loop
foreach(glob('img/radios/big/*.png') as path):
// Not sure what you want to do here. Put the filename instead of
// "1" in the value?
$filename = basename($path, '.png');
?>
<button type="submit"
name="selectedFile"
value="<?=$filename?>">
Go!
</button>
<?
endforeach;
?></form><? // Close the form
In this example I've pulled the form tags out of the loop, because strictly you only need one form. But since there are now no radiobuttons at all, you could also have one form per button. It would still work.
By the way, I saw you mentioned a database in your comment. Of course I didn't take that into account, since you didn't mention that in the question. Anyway, it shouldn't change the basic concept, only the way the 'value' is determined.
First, the value code in radio tag is always 1. So if any radio button is selected, you will always gets 1. Instead the value clause should have the filename. So you get to know which image (radio button) is selected.
For none of the image (radio button) selected, you may use
if (!isset($_POST['radio']))<br>{<br>//php code }
This is my code for texarea that i have.
<textarea class="text" id="post_content_auto" name="post_content_auto"><?php echo (!empty($car_info['post_content_auto']) ? $car_info['post_content_auto'] : ''); ?></textarea>
I want to echo that on some other page and for that i use this code :
<?php echo strip_tags($car_info['post_content_auto']); ?>
But it won't echo text. I tried to change id and name but still no result.
You need to wrap that form element in a form, give it the post method, and an action that points to your PHP file. In your PHP file, access that variable as $_POST['post_content_auto']
I would like to know how to retrieve a selected value from a dynamically selection box. If I get the selected value then I will store it into another variable that is located in another php file. This variable will help me in a sql query in postgresql.
//First php file
<form name="boton_alta_soniador" action="dar_alta_soniador.php" method="POST" autocomplete="off">
<div class="text-field">
Nombre de la asociacion
<? $strconn="dbname=postgres port=5432 host=127.0.0.1 user=xxx password=xxx";
$conn=pg_Connect($strconn);
$consulta="Select n_asociacion from asociacion";
$result=pg_query($conn,$consulta);
while($results [] =pg_fetch_object($result));
array_pop($results);?>
<select name="asociacion_seleccion" id="i_clave_asociacion">
<?php foreach ( $results as $option ) : ?>
<option value="<?php echo $option->i_clave_asociacion; ?>"><?php echo $option->n_asociacion; ?></option>
<?php endforeach; ?>
</select>
</div>
</form>
This is just the dynamically selection box. Then I want to store the selected value in this variable:
$ingresaAsociacion = pg_escape_string($_POST['asociacion_seleccion']);
So I can query the following statement:
$conocerIDasociacion = "SELECT N_ASOCIACION FROM ASOCIACION WHERE I_CLAVE_ASOCIACION='$ingresaAsociacion'";
I didn't want to use jQuery because the whole system is almost entirely made in PHP and HTML.
Please, any help is welcome and I'm all ears to everyone.
Cheers!
Looks like you're missing a Submit button.
You do not have to use AJAX at all, nor jQuery. You can submit your form and process the selection value as you see fit.
The selected value in the select Tag will be sent to dar_alta_soniador.php, and that file will process that data, using the exact code you wrote:
$_POST['asociacion_seleccion']
So, in dar_alta_soniador.php you will write that code:
$ingresaAsociacion = pg_escape_string($_POST['asociacion_seleccion']);
And then perform the query. You do not even have to worry about sending the data around in a session variable, POST does it for you already.
So everything should be OK in your code, or I may have misunderstood your question. Do you have an error message or get some inappropriate behavior?
Maybe the submit button is missing? I use a code like this:
For the select tag:
<div class="controls">
<select name="list_name">
<option>List</option>
<?php
foreach ($nucleos as $inner_array) {
$out = "<option>" . $inner_array['name'] . "</option>";
echo $out;
}
?>
</select>
</div>
And for the Submit button:
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Confirm</button>
<br/>
</div>
</div>
</form>
I am using bootstrap here for style, HTML and CSS. Nothing more.
Best wishes,
I found another solution:
<? $strconn="dbname=postgres port=5432 host=127.0.0.1 user=xxxx password=xxxx";
$conn=pg_Connect($strconn);
$consulta="Select n_asociacion from asociacion";
$result=pg_query($conn,$consulta);?>
<select name="asociacion_seleccion" id="i_clave_asociacion">
<?php
while($row=pg_fetch_assoc($result))
{echo "<option>{$row['n_asociacion']}</option>";}?>
</select>
The point is, whenever the user selects an option from the dyamically selection box, the value of the selection box is known if we call the value with pg_escape_string($_POST['NAME OF YOUR SELECTION BOX']);.
Thanks to all for your collaboration,my problem was resolved.