how to dynamically add / remove form while page refresh using php - php

How to dynamically add and remove input fields the form will stay while page refresh using PHP?. could it possible to use session? please any PHP script for this? thanks in advance!!

You could use something like this
<?php
$showEnterOther = "";
$showEnterOtherAsWell = "";
if(isset($_POST["show_form"])) {
$showEnterOther = "<input type='text' name='whatever' />";
$showEnterOtherAsWell = "<input type='text' name='whatever' />";
}
?>
<form action="#" method="post">
<input type="text" name="username" placeholder="Enter Username" value="<?php if(isset($_POST['username'])){echo $_POST['username'];} ?>"/>
<?php echo $showEnterOther; ?>
<?php echo $showEnterOtherAsWell; ?>
<input type="submit" name="show_form" value="Continue!" />
</form>

Related

Echoed form data and manupulation with GET and POST in PHP

Actually I need to echoed the form and get the form data in the same php page or send to another php page,A pseudo code is prepared as i cannot post my original code here .
<?php echo "<html><body>";
if(isset($_POST['submit']))
{
$name = $_POST['firstname'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
}
echo"<form action=$_SERVER['PHP_SELF']>
First name:<br>
<input type='text' name='firstname' value='John'><br>
Last name:<br>
<input type='text' name='lastname' value='Rambo'><br><br>
<input type='submit' value='Submit'>
</form></body></html>";
?>
The php page is call/loaded successfully loaded .The form is also working.But
form action=$_SERVER['PHP_SELF'] is not working as well as the code below is also not working.
if(isset($_POST['submit']))
{
$name = $_POST['firstname'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
}
This is tested code. Similar to what your want.
Change $_SERVER['PHP_SELF'] to #
<?php
error_reporting(E_ERROR );
$first = '';
$last = '';
if (intval($_POST['sub'])){
$first = $_POST['firstname'];
$last = $_POST['lastname'];
}
echo <<<EOT
<html><head></head><body>
<form action="#" method="post">
First name:<br>
<input type='text' name='firstname' value="$first"><br>
Last name:<br>
<input type='text' name='lastname' value="$last"><br><br>
<input type='submit' value='Submit'/>
<input type="hidden" name="sub" value=1/>
</form></body></html>
EOT;
?>

How do I change a string line submitted in the form with another form input

In my first page I have this code:
$number="1234567891";
$str="456";
echo "<form action='edit.php' method='POST'><input type='hidden' name='msg' value='$message' />
<input type='hidden' name='text' value='$number' />
<input type='hidden' name='edit' value='$str' />
<input type='submit' name='chedit' value='Go' style='position:relative; top:25px; left: 50%;'>
</form>";
In my edit.php I have this code:
<form action="#" method="POST">
Edit Number
<input type="text" name="change" value="$mumu"/>
<input type="submit" name="pch" value="Change"/>
</form>
<?php
if (isset($_POST["chedit"]))
{
$suj = $_POST["msg"];
$text = $_POST["text"];
$mumu =$_POST["edit"];
if(isset($_POST["pch"]))
{
$change = $_POST["change"];
$obinna = str_replace("$change","$mumu","$text");
echo $obinna;
}
}
?>
My problem is that whenever I put a new text in new form and click submit to edit a character in the old string submitted line the page refreshes and no result is output. Please can anybody sort this out?
// try this ..
if (isset($_POST["chedit"]))
{
$suj = $_POST["msg"];
$text = $_POST["text"];
$mumu =$_POST["edit"];
if(isset($_POST["pch"]))
{
$change = $_POST["change"];
//456 , //555(post value) , //(your text)12345678910
// $obinna = str_replace("Set old value you change in text","set new value you want to set in text ","your orignal text ");
$obinna = str_replace("$mumu","$change","$text");
echo $obinna;
}
}
echo '<form action="#" method="POST">
Edit Number
<input type="text" name="change" value="" placeholder="Change"/>
<input type="text" name="edit" value="" placeholder="Edit"/>
<input type="submit" name="pch" value="Submit"/>
</form>'
Check Demo Url :- https://eval.in/931366

HTML reset button not working properly

I have a basic sum example with some PHP and HTML, inside this PHP page the reset button only resetting first two input field, and it is not resetting the third answer field. I dont know what is the error with this cause i am new to PHP. someone please help me to fix this.
code
<html>
<head>
<title>Title goes here</title>
</head>
<body>
<form action="" method="post">
<label>Enter Num1:</label>
<input type="text" name="num1" id="num1" /><br>
<label>Enter Num2:</label>
<input type="text" name="num2" id="num2" /><br><br>
<input type="radio" name="rad" value="add"/>addition
<input type="radio" name="rad" value="sub"/>sub
<input type="submit" name="btn_submit" value="fire">
<?php
if(isset($_POST['btn_submit']))
{
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$rad_val = $_POST['rad'];
if($rad_val=="add"){
$total = $num1+$num2;
}
else if($rad_val=="sub"){
$total = $num1-$num2;
}
echo "<SCRIPT TYPE=\"text/javascript\">
document.getElementById(\"num1\").value= $num1;
document.getElementById(\"num2\").value= $num2;
</SCRIPT>";
echo "<label>Answer is:</label> <input type=\"text\" name=\"rad\" value = $total />";
echo"<input type=\"reset\" name=\"reset\" value=\"reset\" />";
}
else if(isset($_POST['reset'])){
echo "<script>window.location = 'your_page.php';</script>";
}
?>
</form>
</body>
</html>
The reset function loads the your_page.php. So the initial value on that page might be needed to be changed to 0. Could you give an example of your_page.php?
You can trigger a page refresh by changing the line
echo"<input type=\"reset\" name=\"reset\" value=\"reset\" />";
to
echo"<input type=\"reset\" name=\"reset\" value=\"reset\" onclick= \"window.location = 'your_page.php';\"/>";
OR
Even better. Just Reset the value in the Answer to 0, This will save on the page reload.
Add an ID to the result field :
<input type=\"text\" name=\"rad\" id=\"rad\" value = $total />";
And change te value on the reset click/
echo"<input type=\"reset\" name=\"reset\" value=\"reset\" onclick= \" document.getElementById(\"rad\").value= 0;\"/>";
With Jquery you can reset to any value but you net to prevent the default action:
Example:
$('#Reset').click(function(){
event.preventDefault();
$('#element').val('default');
$('#element2').val('default');
$('#element3').val('default');
});

Get variable from another conditional block

I want to get a variable from a conditional if of form assigned to take the value of a textbox:
<form action="" method="POST">
<input type="text" name="name">
<input type="submit" value="Click Here!" name="submit">
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show'])) {
echo $name; //i got "Notice: Undefined variable: name" here
}
?>
</form>
I want show value of $name after input:name pressed.
This should solve the issue
$name = "";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show'])) {
echo $name;
}
The problem in your code is that the scope of $name is limited to the first if
Hello and welcome to stackoverflow,
If you want to make your form in 2 steps, you need to store the "name" value in the intermediate form.
<form action="" method="POST">
<input type="text" name="name">
<input type="submit" value="Click Here!" name="submit">
<?php
if (isset($_POST['submit']))
{
$name = htmlentities($_POST['name']);
echo "<input type=\"hidden\" value=\"{$name}\" name=\"name\">";
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show']))
{
$name = htmlentities($_POST['name']);
echo $name;
}
?>
</form>
Several things to point out :
in a field of type "hidden" you store your $name
in such a way you can recover it in the second step
you should also have a look to the htmlentities() function
Hope this helps!
Try it here

how to pre-load an existing form with a data from db?

i have a problem here,so here's my code
<div id="educmaininfo">
<?php
foreach($cv->getEducation($_GET['cvid']) as $r){
echo "<a href='#' id='editeducation'>Edit</a> | ";
echo "<input type='hidden' name='cvid' value='".$r['ResumeID']."' />";
echo "<input type='hidden' name='educid' value='".$r['EducationID']."'/>";
echo "<a href='#' id='deleteeducation'>Delete</a><br />";
echo "Date From = ".$r['DateFrom']."<br />";
echo "Date To = ".$r['DateTo']."<br />";
echo "Title =".$r['Title']."<br />";
echo "Summary =".$r['Summary']."<br />";
echo "Place Organization =".$r['PlaceOrganization']."<br />";
echo "Emphasis of Study =".$r['EmphasisOfStudy']."<br />";
echo "Study Details =".$r['StudyDetails']."<br />";
echo "Qualifications =".$r['Qualifications']."<br /><br />";
}
?>
</div>
as you can see that code above, I added 2 hidden stuff, the cvid and the educid.
my question is, how to load this form below
<div id="educajaxinfo" style="display:none">
<table>
<form id="educdetails" method="post" action="">
<input type="hidden" name="cvid" id="cvid" value="<?php echo $v['ResumeID']; ?>" />
<tr><td>Date From:</td><td><input type="text" name="datefromeduc" id="datefromeduc" value="" /></td></tr>
<tr><td>Date To:</td><td><input type="text" name="datetoeduc" id="datetoeduc" value="" /></td></tr>
<tr><td>Title:</td><td><input type="text" name="titleeduc" id="titleeduc" value="" /></td></tr>
<tr><td>Summary:</td><td><textarea name="summaryeduc" id="summaryeduc" rows="10" cols="50"></textarea></td></tr>
<tr><td>Place Organization:</td><td><input type="text" name="pog" id="pog" value="" /></td></tr>
<tr><td>Emphasis of study:</td><td><input type="text" name="eos" id="eos" value=""></td></tr>
<tr><td>Study Details:</td><td><textarea name="studyeduc" id="studyeduc" rows="10" cols="50" ></textarea></td></tr>
<tr><td>Qualifications:</td><td><textarea name="qualificationseduc" id="qualificationseduc" rows="10" cols="50"></textarea></td></tr>
<tr><td><input type="submit" name="update" value="<?php if($count < 3){ echo 'Add';}else{ echo 'Update';}?>" /></td></tr>
</form>
</table>
</div>
with the existing data from the db table, that matches the cvid and educid ?
the flow goes like this, if user clicks the edit link, it should redirect him to the
form with the values loaded in the input forms...if in pure php i can do this by just
doing something like
e.g edit.php?cvid=cvid&educid=educid
how to do it in ajax way?
Use jQuery to call a PHP script that returns JSON encoded data. Then use the “success” callback function to inject the data into your form.
Use the jquery function like following
a href='#' onClick=test
(echo $cvid ,echo $educid ) id='editeducation'>Edit
function test(cvid,educid)
{
jQuery.post('edit.php?cvid=cvid&educid=educid',function(response){
jQuery('#educajaxinfo').html(response);
});
}
include the appropriate jquery min file to use above function.
on edit.php get the two id's and prepare a whole html then it will be shown in #educajaxinfo div

Categories