I haven't come to much luck finding anything on this, since I couldn't think how to word it originally.
Basically I have a form, in HTML, the end user will submit a value such as "12345_54321" into 1 input field, then process.
I would like to be able to allow there to be 2 input fields instead, so one of them they would enter "12345" and in the second one, they'd enter "54321".
Which seems easy enough, but my real need is that the "_" must be used as a separator, such as, when the value is submitted, it will process "12345_54321" instead of "12345" and "54321"
My form so far:
<form role="form" method="post" action="process.php">
<fieldset>
<div class="form-group">
<input size="18" type="visible" name="postid" id="postid" class="form-control" placeholder="Enter Story ID Here:" class="input-medium" ><input size="18" type="visible" name="postid" id="postid" class="form-control" placeholder="Enter Comment ID Here:" class="input-medium" >
</div>
<input type="submit" name="submit" class="btn btn-primary btn-large" id="submit_btn" value="Process"/>
</fieldset>
If you want to display 2 inputs for the story id, you have to modify your page to be like so (no need for the underscore (_):
<form role="form" method="post" action="process.php">
<fieldset>
<div class="form-group">
<input size="18" type="visible" name="story_id_1" id="story_id_1" class="form-control" placeholder="Enter Story ID Here:" class="input-medium" >
<input size="18" type="visible" name="story_id_2" id="story_id_2" class="form-control" placeholder="Enter Story ID Here:" class="input-medium" >
<input size="18" type="visible" name="comment_id" id="comment_1" class="form-control" placeholder="Enter Comment ID Here:" class="input-medium" >
</div>
<input type="submit" name="submit" class="btn btn-primary btn-large" id="submit_btn" value="Process"/>
</fieldset>
</form>
In your process.php you can get these variables by looking in your POST.
<?php
$story_id_1 = '';
$story_id_2 = '';
$comment_id = '';
// Check for empty fields
if(isset($_POST['story_id_1']))
$story_id_1 = $_POST['story_id_1']; // From HTML Page
if(isset($_POST['story_id_2']))
$story_id_2 = $_POST['story_id_2']; // From HTML Page
if(isset($_POST['comment_id']))
$comment_id = $_POST['comment_id']; // From HTML Page
print 'Story Id 1: '. $story_id_1 . '</br>';
print 'Story Id 2: '. $story_id_1 . '</br>';
print 'Comment Id: '. $comment_id . '</br>';
Add a hidden field, whose value will be the concatenation of the two input fields:
and on submit, set its value by concatenating the values of the two input fields:
<form role="form" method="post" action="process.php">
<fieldset>
<input type="hidden" id="theValue" />
<div class="form-group">
<input size="18" type="visible" name="postid" id="postid1" class="form-control" placeholder="Enter Story ID Here:" class="input-medium" onchange="concat();">
<input size="18" type="visible" name="postid" id="postid2" class="form-control" placeholder="Enter Comment ID Here:" class="input-medium" onchange="concat();">
</div>
<input type="submit" name="submit" class="btn btn-primary btn-large" id="submit_btn" value="Process" />
</fieldset>
<script>
// if no jQuery.....standard ECMAScript
function concat() {
var val = document.getElementById('postid1').value + '_' + document.getElementById('postid2').value;
document.getElementById('theValue').value = val;
console.log(val);
}
Related
I need to create a table of 17 rows where each row contains information such as row number, name, surname, email and birthday. The data is provided by this form:
<form action="index.php" method="post">
<input type="text" name="name" placeholder="name" />
<input type="text" name="surname" placeholder="surname" />
<input type="text" name="emailbirthday" placeholder="emailbirthday" />
<input type="text" name="birthday" placeholder="birthday(dd/mm/aaa)" />
<button type="reset">Reset Form</button>
<button type="submit">Submit Form</button>
</form>
After clicking submit the data should be displayed in the nth row of the table(row number one if it is the first "pack" of data submitted, number two if its the second and so on). This problem could easely be solved using databases but i cannot use them(by professors order).
I tried to create an array than push values into it like this:
$array_name = array();
$name = $_POST["name"];
array_push($array_name, $name);
This approach doesn't work(the index of the array stays 0 alla of the time so it keeps replacing the first value again and again) and manually incrementing the index counter of the array doesn't work either.
Normally one should use a database approach but your professor explicitly forbids it.
There are many other ways to do it. (store as TEXT/JSON/CSV file or localstorage / cookies), etc. For me I would use session to do the job
declare a session variable which is an array
if user submits the form, store the POST data into another array (subarray) containing name, surname, birthday, email
add the subarray into the main session variable array
print it out at the end as a table
So the PHP will be:
<?php
session_start();
?>
<form action="#" method="post">
<input type="text" name="name" placeholder="name" />
<input type="text" name="surname" placeholder="surname" />
<input type="text" name="email" placeholder="email" />
<input type="text" name="birthday" placeholder="birthday(dd/mm/aaa)" />
<button type="reset">Reset Form</button>
<button type="submit">Submit Form</button>
</form>
<?php
if (!isset($_SESSION["arr"])){
$_SESSION["arr"]=array();
}
if ($_POST) {
$subarray=array(
"name"=>$_POST["name"],
"surname"=>$_POST["surname"],
"birthday"=>$_POST["birthday"],
"email"=>$_POST["email"]
);
$_SESSION["arr"][]=$subarray;
}
echo "<table border=1><tr><td>Name<td>Surname<td>Email<td>Birthday";
foreach($_SESSION["arr"] as $suba){
echo "<tr><td>" . $suba["name"] ;
echo "<td>" . $suba["surname"] ;
echo "<td>" . $suba["email"] ;
echo "<td>" . $suba["birthday"] ;
}
echo "</table>";
?>
However, if you need the data to be persistent (even after the user closes the browser), then you need to store the data say in file format or cookies, etc.
If you need to save data persistent and using file to save data is acceptable, i'd use something like that:
<?php
$file = 'path/to/file.txt';
$data = json_decode(file_get_contents($file), true);
if ($_POST) {
$data[] = [
"name" => $_POST['name'],
"surname" => $_POST['surname'],
"emailbirthday" => $_POST['emailbirthday'],
"birthday" => $_POST['birthday']
];
}
file_put_contents($file, json_encode($data));
?>
<form action="index.php" method="post">
<input type="text" name="name" placeholder="name" />
<input type="text" name="surname" placeholder="surname" />
<input type="text" name="emailbirthday" placeholder="emailbirthday" />
<input type="text" name="birthday" placeholder="birthday(dd/mm/aaa)" />
<button type="reset">Reset Form</button>
<button type="submit">Submit Form</button>
</form>
<table>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Emailbirthday</th>
<th>Birthday</th>
</tr>
<?php
foreach ($data as $row) {
print '<tr>
<td>'.$row['name'].'</td>
<td>'.$row['surname'].'</td>
<td>'.$row['emailbirthday'].'</td>
<td>'.$row['birthday'].'</td>
</tr>';
}
?>
</table>
You can use the post values of hidden fields:
<form action="" method="post">
<input type="text" name="name" placeholder="name" />
<input type="text" name="surname" placeholder="surname" />
<input type="text" name="emailbirthday" placeholder="emailbirthday" />
<input type="text" name="birthday" placeholder="birthday(dd/mm/aaa)" />
<button type="reset">Reset Form</button>
<button type="submit">Submit Form</button>
<?php
if($_POST["names"] == "")
{
$value = $_POST["name"];
}
else
{
$value = $_POST["names"]."-".$_POST["name"];
}
?>
<input type="text" name="names" style='display:none;' value="<?php echo $value ?>">
</form>
this is a page that displays a list of creatives, and the form offers search functionality to search by job title:
if(isset($_POST['creatives-submit'])){
$job = $_POST['job-title'];
$data = \Db::Common($fms5->DBH)->getWhere("creatives", "creatives_active", "Yes"," AND creatives_job LIKE '%".$job."%'")->orderBy('creatives_name', 'asc');
}
<form method="post" name="creative-search">
<input class="form-control" type="textbox" name="job-title" id="job-title" placeholder="Search by job title" />
<input class="form-control" type="submit" name="creatives-submit" id="creatives-submit" style="display: none;" />
</form>
is there anything that's obviously wrong my my code?
try changing if(isset($_POST['creatives-submit'])) to if(isset($_POST['job-title']) && !empty($_POST["job-title"])) as the form is posting the job-title value and this is the value you actually care about. (Since creatives-submit will always = Submit)
also change
<input class="form-control" type="textbox" name="job-title" id="job-title" placeholder="Search by job title" />
to <input class="form-control" type="text" name="job-title" id="job-title" placeholder="Search by job title" required/>
this means the form can't be submitted unless the job-title field has a value and had the correct type of text
Below is a modification of your code that just returns what the user searched for (Since I don't have it connected to a database)
<?php
if(isset($_POST['job-title']) && !empty($_POST["job-title"])){
$job = $_POST['job-title'];
?>
<p>You Searched For <?php echo $job;?></p>
<?php
}
?>
And the form
<!-- Search Form -->
<form method="post" name="creative-search">
<input class="form-control" required="required" type="text" name="job-title" id="job-title" placeholder="Search by job title" />
<input class="form-control" type="submit" name="creatives-submit" id="creatives-submit" style="display: none;" />
</form>
I have a form an need replace button in my form with another data. I should use php regex and replace it. My form sample this is:
<form name="ialRegister" id="ialRegister" method="post">
<input type="text" name="name" id="name">
<input type="text" name="email" id="email">
<input type="text" name="mobile" id="mobile">
<label data-attr="subtitle" class="smallTxt hidden" for="ialButton10"></label>
<button class="loginBtn ial-submit" name="submit" id="ialButton10">
<span><i class="ial-load" style="visibility: hidden;"></i>
<span data-attr="label">Register</span></span>
</button>
</form>
and I used this regex but it's not working:
$newField = 'custom data';
$form = preg_replace("#(<form.*id=\"ialRegister\".*>.*)
<button.*name=\"submit\".*>.*<\/button>(.*<\/form>)#sU", '$1'.$newField.'$2', $form);
How can do it?
I'm writing a php form that has a button to test the database connection before proceeding to the next step. The only problem is that running the test, clears the fields. I can put onsubmit="return false" at the top of the form, but then the test works fine, but I can't submit the form for its real purpose then. I'm guessing this could be fixed with javascript, but I'm a total noob there and I'm wondering if there is a PHP/HTML way to accomplish this.
Here's the form. The test button runs a test pdo connection and gives feedback and the "Next Step >>" button writes the configuration to a file and goes on to the next step.
<H2>Please fill in your database credentials</H2>
<form class="form" action="" onsubmit="return false" method="post">
<label for="dbh">Database Host
<input class="form-control" type="text" name="dbh" value=""></label>
<br><br>
<label for="dbu">Database User
<input class="form-control" type="text" name="dbu" value=""></label>
<br><br>
<label for="dbp">Database Password
<input class="form-control" type="text" name="dbp" value=""></label>
<br><br>
<label for="dbn">Database Name
<input class="form-control" type="text" name="dbn" value=""></label>
<br><br>
<input class="btn btn-success" type="submit" name="test" value="Test Settings">
<input class="btn btn-primary" type="submit" name="submit" value="Next Step >>">
</form>
You need to write in $_POST vars in your inputs.
<label for="dbh">Database Host
<input class="form-control" type="text" name="dbh" value="<? if ($_POST['dbh']){ print $_POST['dbh']; } ?>"></label><br><br>
<label for="dbu">Database User
<input class="form-control" type="text" name="dbu" value="<? if ($_POST['dbu']){ print $_POST['dbu']; } ?>"></label><br><br>
<label for="dbp">Database Password
<input class="form-control" type="text" name="dbp" value="<? if ($_POST['dbp']){ print $_POST['dbp']; } ?>"></label><br><br>
<label for="dbn">Database Name
<input class="form-control" type="text" name="dbn" value="<? if ($_POST['dbn']){ print $_POST['dbn']; } ?>"></label><br><br>
I wasn't sure how to phrase the title, but here's what I'm trying to do.
I have a form to login to webmail, I don't have access to the webmail - it just posts the form input to the website and the webmail server takes it from there.
Everyone logging in to this page will be using the same email extension (e.g. "#myemail.com"), so I want to save the hassle of typing that every time, instead they can just write "mike" and the form will add "#myemail.com" on it's own.
I could post this form to a middle ground php page that sticks "mike" + "#mymail.com" together and posts this info to the webmail?
Or is there a simpler way to do this without having to create another page?
<form name="loginForm" action="http://webmail.emailsrvr.com/login.php" method="POST" target="_blank">
<input type="hidden" name="js_autodetect_results" value="SMPREF_JS_OFF">
<input type="hidden" name="just_logged_in" value="1">
<input type="hidden" name="type" value="v3">
<input type="hidden" name="useSSL" id="useSSL" value="">
<input type="email" name="user_name" placeholder="Username" required autofocus>
<input type="password" name="password" placeholder="Password" required>
<label for="remember"><input type="checkbox" name="remember">Remember my info</label>
<button type="submit">Sign In</button>
</form>
I want to take the value entered here...
<input type="email" name="user_name" placeholder="Username" required autofocus>
...and add an extension like "#myemail.com" to it, then post it.
Any idea? Thanks in advance for your help.
I could do something like this?
PAGE 1 - Enter username and password...
<input type="text" name="send_email" placeholder="Username" required autofocus>
<input type="password" name="send_password" placeholder="Password" required>
<label for="remember"><input type="checkbox" name="remember">Remember my info</label>
<button type="submit">Sign In</button>
</form>
PAGE 2 - PHP takes username and adds "#myemail.com" and sends form...
<?php
$form_email = $form_password = "";
if (!empty($_POST['send_email']) && !empty($_POST['send_password'])) {
$form_email = $test($_POST['send_email']) . '#fountaincreations.ca';
$form_password = $test($_POST['send_password']);
echo '<form name="loginForm" action="http://webmail.emailsrvr.com/login.php" method="POST" target="_blank">';
echo '<input type="hidden" name="js_autodetect_results" value="SMPREF_JS_OFF">';
echo '<input type="hidden" name="just_logged_in" value="1">';
echo '<input type="hidden" name="type" value="v3">';
echo '<input type="hidden" name="useSSL" id="useSSL" value="">';
echo '<input type="hidden" name="user_name" value="' . $form_email . '">';
echo '<input type="hidden" name="form_password" value="' . $form_password . '">';
echo '</form>';
} else {
echo '<p style="text-align:center;padding:40px 20px;">Please go back and try again.</p>';
}
function test($data) {
$data = strtolower(data);
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
How do I tell PHP to automatically submit the form though?
Here's what I have if I were to use jQuery:
It doesn't currently work though.
HTML:
<div class="box top">
<form name="loginForm" action="http://webmail.emailsrvr.com/login.php" method="POST" target="_blank">
<input type="hidden" name="js_autodetect_results" value="SMPREF_JS_OFF">
<input type="hidden" name="just_logged_in" value="1">
<input type="hidden" name="type" value="v3">
<input type="hidden" name="useSSL" id="useSSL" value="">
<div class="inner">
<div class="inner-row"><input type="text" name="user_name" placeholder="First Name" required autofocus></div>
<div class="inner-row"><input type="password" name="password" placeholder="Password" required></div>
<div class="inner-row">
<div class="inner-col col-2-4"><label for="remember"><input type="checkbox" name="remember">Remember my info</label></div>
<div class="inner-col col-2-4"><button type="submit">Sign In</button></div>
</div>
</div>
</form>
</div>
<div class="box bottom">
<div class="inner">
<div class="inner-row"><p>Already signed in? <b>Go to your inbox.</b></p>
</div>
</div>
JS:
<script type="text/javascript">
$(document).ready(function(){
$("button[type='submit']").on('click', function(e){
e.preventDefault();
var userEmail = $.trim($("input[name='user_name']").val());
$("input[name='user_name']").val(userEmail+"#fountaincreations.ca");
$("form[name='loginForm']").submit();
});
});
</script>
I think if you want to attach username + "#mymail.com" to useremail field, here is my suggestion:
1# create input user_email (just hide this field)
<input type="hidden" name="user_email" id="user_email" placeholder="User Email">
2# add js below
$('#user_name').on('input', function() {
username = this.value+'#myemail.com';
$('#user_email').val(username);
});
here example
I think this will go like this, hope this will help you. What i am doing in this code is, first I stop the form from being submitted through e.preventDefault(); and then i am taking the value of the required input and assigning it back value by attaching "#mymail.com".
$(document).ready(function(){
$("button[type='submit']").on('click', function(e){
e.preventDefault();
var userEmail = $.trim($("input[type='email']").val());
$("input[type='email']").val(userEmail+"#mymail.com");
$("form[name='loginForm']").submit();
});
});