How pass variables for alternative if -syntax? - php

<?php
$a=3;
$dir= '/php/';
?>
<?php if($a ==3){ ?>
<form action="/php/form.php" method="post">
<p>Name: <input type="text" name="name" /></p>
<p>Age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
<?php } ?>
I want to pass $dir in form action ,like action=$dir.form.php. It's possible?

Yes it is possible you need to simply pass $dir to action Try
<form action="<?php echo $dir;?>form.php" method="post">

You can embed php tags anywhere in your html code. Rewrite your snippet like this:
<?php
$a=3;
$dir= '/php/';
?>
<?php if($a ==3){ ?>
<form action="<?php echo $dir."form.php" ?>" method="post">
<p>Name: <input type="text" name="name" /></p>
<p>Age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>

Yes it is possible
<?php
$a=3;
$dir= '/php/';
if($a ==3){ ?>
<form action="<?php echo $dir.'form.php'; ?>" method="post">
<p>Name: <input type="text" name="name" /></p>
<p>Age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
<?php } ?>

Related

Submit form value return

I have this source code...
<form method="post" id="center" action="">
<br>SpielerName: <?php echo $SpielerName; ?>
<br>Note: <input type="text" name="note" value=<?php echo $Note ?> >
<br>Tore: <input type="text" name="tore" value=<?php echo $Tore ?> >
<br><br><input type="submit" name="submit_eingabemaskeR" value="Abschicken">
In the following code I get the values for 'note'...
if (isset($_POST["submit_eingabemaskeR"]))
{
echo ("<br/>");
//Note
echo $_POST["note"];
But how can I echo the value of the first field -> SpielerName?
SpielerName is not a form field, its just text.
If you want its data submitted you can make a hidden form field with that value.
<form method="post" id="center" action="">
<br>SpielerName: <?php echo htmlspecialchars($SpielerName); ?>
<input type="hidden name="SpielerName" value="<?php echo htmlspecialchars($SpielerName); ?>">
<br>Note: <input type="text" name="note" value=<?php echo htmlspecialchars($Note) ?> >
<br>Tore: <input type="text" name="tore" value=<?php echo htmlspecialchars($Tore) ?> >
<br><br><input type="submit" name="submit_eingabemaskeR" value="Abschicken">
Use a hidden input.
<form method="post" id="center" action="">
<br>SpielerName: <?php echo $SpielerName; ?>
<input type="hidden" name="SpielerName" value=<?php echo $SpielerName; ?> >
<br>Note: <input type="text" name="note" value=<?php echo $Note ?> >
<br>Tore: <input type="text" name="tore" value=<?php echo $Tore ?> >
<br><br><input type="submit" name="submit_eingabemaskeR" value="Abschicken">

php form post array order

In php, if you name your form fields using numerical indexes, they work as arrays in the $_POST object.
<form method="post" action="post.php">
<input type="text" name="question[0][name]" />
<input type="text" name="question[0][email]"/>
<input type="text" name="question[0][password]" />
<hr>
<input type="text" name="question[1][name]" />
<input type="text" name="question[1][email]"/>
<input type="text" name="question[1][password]" />
<hr>
<input type="submit" value="Add" />
<hr>
<p><?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo json_encode($_POST, JSON_NUMERIC_CHECK);
}
?></p>
</form>
outputs
{"question":[{"name":"a","email":"aa","password":"aaa"},{"name":"b","email":"bb","password":"bbb"}]}
If the ordering of the fields is not sequential starting at zero and incrementing by only one each time the name is repeated, then they are all interpreted as keys instead. So
<form method="post" action="post.php">
<input type="text" name="question[1][name]" />
<input type="text" name="question[1][email]"/>
<input type="text" name="question[1][password]" />
<hr>
<input type="text" name="question[0][name]" />
<input type="text" name="question[0][email]"/>
<input type="text" name="question[0][password]" />
<hr>
<input type="submit" value="Add" />
<hr>
<p><?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo json_encode($_POST, JSON_NUMERIC_CHECK);
}
?></p>
</form>
outputs
{"question":{"1":{"name":"a","email":"aa","password":"aaa"},"0":{"name":"b","email":"bb","password":"bbb"}}}
Is there a way to get $_POST to ignore the order of arrays of post keys so they are interpreted as an array?
Please check if helpful or not :
<form method="post" action="#">
<input type="text" name="question[1][name]" />
<input type="text" name="question[1][email]"/>
<input type="text" name="question[1][password]" />
<hr>
<input type="text" name="question[0][name]" />
<input type="text" name="question[0][email]"/>
<input type="text" name="question[0][password]" />
<hr>
<input type="submit" value="Add" />
<hr>
<p>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
ksort($_POST['question']);
print_r($_POST['question']);
}
?>
</p>
</form>

connection between php scripts and html input

<form id="submit-form" method="post" action="" enctype="multipart/form-data">
<label>dateField1</label>
<?php
print "<b>calendar:</b><br/>";
$dateField1 = new dateField($format,"date1",$img);
$dateField1->setTitles($arr_daysOfTheWeek,$arr_months,$format_title);
$dateField1->setCssClasses($arr_cssClasses);
print "value:" . $dateField1->makeDateField();
?>
<input type="text" class="span3" name="dateField1" tabindex="2" value=" " />
</form>
I want to make that value in php script ... be related to the value of the input form html
I tried so many ways ,but not implemented , please help
Try it like
PHP in HTML :
<input type="text" class="span3" name="dateField1" value="<?php echo $dateField1->makeDateField();?>" />
Or even you can try like
HTML in PHP :
<?php echo '<input type="text" class="span3" name="dateField1" value="'. $dateField1->makeDateField().'" />';?>
Try this:
<form id="submit-form" method="post" action="" enctype="multipart/form-data">
<label>dateField1</label>
<?php
print "<b>calendar:</b><br/>";
$dateField1 = new dateField($format,"date1",$img);
$dateField1->setTitles($arr_daysOfTheWeek,$arr_months,$format_title);
$dateField1->setCssClasses($arr_cssClasses);
$value = $dateField1->makeDateField();
?>
<input type="text" class="span3" name="dateField1" tabindex="2" value=" <?php echo $value; ?> " />
</form>
You can use php inside html by starting php tags within html
<form id="submit-form" method="post" action="" enctype="multipart/form-data">
<label>dateField1</label>
<?php
print "<b>calendar:</b><br/>";
$dateField1 = new dateField($format,"date1",$img);
$dateField1->setTitles($arr_daysOfTheWeek,$arr_months,$format_title);
$dateField1->setCssClasses($arr_cssClasses);
print "value:" . $dateField1->makeDateField();
?>
<input type="text" class="span3" name="dateField1" tabindex="2" value="<?php echo $value;?>" />
</form>

TwoPHP blocks in file

i have two HTML forms and two PHP blocks in one file (index.php). for example i want the second php script belonged to the second form. i dont know, how to do it. What i write to the action atribute ?
here is my code:
<form method="post" action="htmlspecialchars $_SERVER ["PHP_SELF"]">
<input type="text" name="name"> <br>
<input type="submit">
</form>
<form method="post" action="htmlspecialchars $_SERVER ["PHP_SELF"]">
<input type="text" name="age"> <br>
<input type="submit">
</form>
<?php
echo $_POST ["name"];
?>
<?php
echo $_POST ["age"];
?>
Hope it helps you,
First form,
<form method="post" action="<?php echo $_SERVER ["PHP_SELF"];?> ">
<input type="text" name="name"> <br>
<input type="submit" name='submit' >
</form>
<?php
if(isset($_POST['submit'])){
echo $_POST ["name"];
}
?>
Second form
<form method="post" action="<?php echo $_SERVER ["PHP_SELF"];?>">
<input type="text" name="age"> <br>
<input type="submit" name='submitsecond' > // name submitsecond indicates as second form
</form>
<?php
if(isset($_POST['submitsecond'])){
echo $_POST ["age"];
}
?>
You can use a hidden input field to distinguish both scripts. And you'll have to echo/print the script name ($_SERVER['PHP_SELF']), htmlspecialchars is not needed...
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" name="form" value="name_form" />
<input type="text" name="name"> <br>
<input type="submit">
</form>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" name="form" value="age_form" />
<input type="text" name="age"> <br>
<input type="submit">
</form>
<?php if($_POST['form'] == 'name_form'): ?>
The name form is submitted.<br>
Name: <?php echo $_POST['name']; ?>
<?php endif; ?>
<?php if($_POST['form'] == 'age_form'): ?>
The age form is submitted.<br>
Age: <?php echo $_POST['age']; ?>
<?php endif; ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER ["PHP_SELF"]); ?>">
<input type="text" name="name"> <br>
<input type="submit" name="name_sub">
</form>
<?php
if(isset($_POST ["name_sub"])) // check if name form is submit
echo $_POST ["name"];
?>

How do you use php?email=# to retrieve record

In abc.php, i have a form whose action is xyz.php. I want $email=$POST['txtEmail'] to be used in xyz.php
To do that, I typed: <form id="form1" name="form1" method="post" action="xyz.php?<?php $email=$_POST['txtEmail']; ?>">
Is this the correct way to do it?
EDIT
abc.php
<form id="form1" name="form1" method="post" action="xyz.php">
<input name="txtEmail" type="text" size="30" />
<input type="submit" name="Submit2" value="Submit" />
</form>
xyz.php
<form id="form1" name="form1" method="post" action="def.php">
<input type="text" name="txtAns" />
<input type="hidden" name="txtEmail" id="txtEmail" value="<?php echo $_POST['txtEmail']; ?>" />
<input type="submit" name="submitEmail" value="Submit" />
</form>
def.php
$email=$_POST['txtEmail'];
Edited
In abc.php:
<form id="form1" name="form1" method="post" action="xyz.php">
<input type="text" name="email" id="email" />
</form>
In xyz.php
<form id="form2" name="form2" method="post" action="def.php">
<input type="hidden" name="email" id="email" value="<?php echo $_POST['email']; ?>" />
</form>
In def.php
$email = $_POST['email'];
Is that what you're after?

Categories