Can i insert the value in the php include function? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I already have this code for showing the country list:
<label><div>Country</div>
<?php include('includes/countrylist.php'); ?>
</label>
and also have data of the country like this $tourdetail['country'].
So, how to show the value of the country in input box?
Sample code for input box:
<?= form_input(array('name' => 'country', 'value' => $tourdetail['country'])); ?>

In your example i think you are using $tourdetail['country'] inside any function which you wrote in includes/countrylist.php. so re declare the variable like this inside your function.
global $tourdetail;
<?php
//declare a variable before including
$temp="test";
?>
<label><div>Country</div>
<?php include('includes/countrylist.php'); ?>
</label>
includes/countrylist.php
...
global $temp; //if you are using inside any function make it as global
echo $temp;
...
This $temp variable will be accessible inside your countrylist.php

No, You can't bro. You can't put input box into include function. You need to use include and input separately. If you just want country with drop down so you can try like the following.
<?php
include('includes/countrylist.php');
foreach ($tourdetail['country'] as $country) {
echo "<option value='". $country ."'>". $country ."</option>";
}
?>

Yes, I understood that you would like to display the country values in dropdown box.
Please find the below code:
In your include file, you already defined your country list as an array list "$tourdetail['country']".
<select name="country">
<?php
include('includes/countrylist.php');
foreach ($tourdetail['country'] as $country) {
echo "<option value='". $country ."'>". $country ."</option>";
}
?>
</select>

Please see this
<input name="country" type="text" class="text_box" required id="country" value="<?=isset($tourdetail['country']!=''?$tourdetail['country']:'')?>" />

Related

How can I get the selected value from my <select> [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I try to get the value from the select area using form and $_Post. I looked through plenty of older questions and am sure, I followed the instructions. still i get the error: Undefined array key "platz".
<form action="" method="POST">
<select name="platz">
<?php
for ($i=0; $i < $saalinfo[2]; $i++) {
echo "<option value='$i'> Reihe $i </option>";
}
?>
</select>
<select class="" name="reihe">
<?php
for ($i=0; $i < $saalinfo[3]; $i++) {
echo "<option value='$i'> Spalte $i </option>";
}
?>
</select>
<?php echo "($_POST[platz])" ?>
</form>
For clarification Saalinfo 2 and 3 contain integer so that there are different options to choose from based on the given values. Many Thanks in advance.
I also tried
<?php echo "($_POST['platz'])" ?>
When the page loads initially there will be no POST data so you get that error. Instead check that POST array is available first - like so perhaps:
<?php echo !empty( $_POST['platz'] ) ? $_POST['platz'] : ''; ?>

Printing array output in PHP in a readable format [duplicate]

This question already has answers here:
Is there a pretty print for PHP?
(31 answers)
Closed 7 months ago.
I'm trying to print multiple values user has selected on form submit. However with following what I'm seeing is only the last element printed irrespective whether it is selected or not.
Note that the print on the screen I'm looking at is a print that a layman can understand!
<?php
if(isset($_POST['submit'])) {
//I'm trying to show the user these are the values you've selected
print_r($option['name']);
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]);?>">
<td class="container">
<select multiple name="mercha_A[]" class="selectpicker form-control" title="Merchandiser type">
<?php foreach ($options as $option) { ?>
<option value="<?php echo $option['value']; ?>" <?php echo (isset($_POST[ 'mercha_A']) && in_array($option[ 'value'], $_POST[ 'mercha_A'])) ? ' selected="selected"' : ''; ?>>
<?php echo $option['name']; ?>
</option>
<?php } ?>
</select>
</td>
<td><button type="submit" name="submit">Submit</button></td>
</form>
Anyone needs a coffee on my account?
wrap your print_r in <pre> Tags
echo "<pre>";
print_r($option['name']);
echo "</pre>;
echo "<pre>";
print_r($_POST['mercha_A']); // you have to print the name attribute not option
echo "</pre>;
depending on your situation, you could use either of these, I think the last will best suite those who don't have a programming background. Because, I think JSON is a human readable format.
Method 1:
echo '<pre>'; print_r($_POST['mercha_A']); echo '</pre>';
Method 2:
echo json_encode($_POST['mercha_A']);
I think you meant to print
$_POST['mercha_A'];
Otherwise, $option['name'] is completely undefined in your case, but even if you put the print_r() at the end of the script, it would only be the name of the last option in $options.
In order to make print_r() readable, you can View Source (Ctrl+U) in your browser, or wrap it in <pre></pre> tags.
Using extbase debugger from TYPO3.
Check it out it's insane :) https://github.com/TYPO3/TYPO3.CMS/blob/master/typo3/sysext/extbase/Classes/Utility/DebuggerUtility.php
It helps you to debug arrays and object in a readable way
DebuggerUtility::var_dump($array)

How do I Get This HTML 5 Form To Give PHP The Input Data Then Display It On The Screen [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How do I Get This HTML 5 Form To Give PHP The Input Data Then Display It On The Screen. I want to collect data in a form. Convert it into a php variable and then echo it out in a way that I can read it.
<form>
First name:<br>
<input type="text" name="name">
</form>
<body>
<pre>
<?php
$taco = htmlspecialchars($_POST["name"]) . '!';
$taco = $_POST;
echo $taco;
?>
</pre>
</body
</html>
How about the following:
<?php
if($_POST) { // check if the page is posted not with a $_GET
foreach($_POST as $field => $value) { // Foreach field
$value = strip_tags($value); // Strip html tags (you can add more)
echo 'Field: '.$field.' has value: '.$value.'<br>'; // output
}
}
?>
<form action="" method="post">
First name: <input type="text" name="first_name">
<input type="submit" value="Display">
</form>
Just put it all in one php file. You can add as many fields as you want.

PHP selected item [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
i got this script below. How do i get the answer from the selected item?
how can i get the option back with the $_POST?
<?php
mysql_select_db("internetsites");
$query1 = "SELECT * FROM internetsites ORDER BY name_site";
$result = mysql_query($query1) or die(mysql_error());
?>
<form name="delete" action="delete.php" method="post">
choose a site you want to delete.
<select>
<?php
while($row = mysql_fetch_array($result))
{
echo "<option value=" . $row['name_site'] . "'>" . $row['name_site'] . "</option>";
}
?>
</select>
<input type="submit" value="delete">
Can someone help me?
You give your select a name atribute, then you take the whole code inside <form method="post" action="delete.php"></form> and then you use $_POST['nameattribute'].
Try this, To Post form elements need input name attribute,
<form name="delete" action="delete.php" method="post">
choose a site you want to delete.
<select name="name_site" id="name_site">
<?php
while($row = mysql_fetch_array($result))
{
echo "<option value='".$row['name_site']."'>" . $row['name_site'] . "</option>";
}
?>
</select>
<input type="submit" name="submit" value="delete">
</form>
in delete.php,
<?php
if(isset($_POST['submit'])){
echo $_POST['name_site'];
}
?>

Storing selected drop down list value in a variable in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have accessed a MySql database, retrieved the values from the relevant table and put them in a drop down list using:
$material_query= "SELECT material FROM materials";
$material_query_run = mysql_query($material_query);
echo "<select>";
while ($material_query_array= mysql_fetch_array($material_query_run) ){
echo "<option value='' >".$material_query_array['material']."</option>";
}echo "</select>";
How would I now store the selected value from the drop down list within a variable? I think that I need to use POST however, I cannot figure out how.
So the select element needs to be inside a form, which you can then submit and the data submitted (by post or get) can then be processed.
Your select box needs to have a name attribute so that it can be identified. Also you need to have a value in the value attribute of the option elements, as this is the data that is sent.
For example, on your page (e.g. page.php) you would have your current code inside html form tags:
// The Form
<form action="page.php" method="post">
<?php
$material_query = "SELECT material FROM materials";
$material_query_run = mysql_query( $material_query );
echo "<select name='mySelect'>";
while ( $material_query_array = mysql_fetch_array( $material_query_run ) ) {
echo "<option value='".$material_query_array['material']."' >".$material_query_array['material']."</option>";
}
echo "</select>";
?>
<input type="submit" name="submit"/>
</form>
//Process the form
//check if form is submitted
if ( isset( $_POST['submit'] ) ) {
//is submitted
$variable = $_POST['mySelect'];
//DO STUFF WITH DATA
}
So here I have done the following:
Added the form tags ( SEE: http://www.w3schools.com/php/php_forms.asp )
Added the name attribute to the select tags
Added the same value that the select displays to the value attribute of the option tags
Added code to process the form when it is submitted ( See the above link again )
When the user hits the submit button, the data will need to be sent to your PHP processor, either using GET or POST. In your processor, then you would access the SELECT field values just like any other form element.
<form action="processor.php" method="POST">
// FORM ELEMENTS HERE
<input type="submit" value="Go!">
</form>
In your processor:
<?php
$selectbox = $_POST['selectbox'];
Now you can sanitize and use the variable $selectbox in your script or pass it to your database.
You need something like:
<form action="post.php" method="post" name="select_form">
<?php
$material_query="SELECT material FROM materials";
$material_query_run =mysql_query($material_query);
echo "<select name=\"selectbox\">";
while ($material_query_array= mysql_fetch_array($material_query_run) ){
echo "<option value='".$material_query_array['material']."'>".$material_query_array['material']."</option>";
}
echo "</select>";
?>
<input type="submit" value="Submit" name="submit">
</form>
Then in post.php
<?php
if($_POST){
$select=$_POST['selectbox'];
}
?>
On a side note use PDO (http://www.php.net/manual/en/book.pdo.php) or MySQLI (http://uk3.php.net/manual/en/book.mysqli.php) as the MySql interface is outdated.

Categories