Situation:
The form is at https://example.com
If user write "demo" in input then submit, they will be redirected to
https://demo.example.com where "demo" is from the value they've putted before.
How to do that?
My code:
<form class="form-inline" action="http://example.com" method="GET">
<div class="form-group">
<label class="sr-only" for="exampleInputAmount">Login</label>
<div class="input-group">
<div class="input-group-addon">https://</div>
<input type="text" class="form-control" id="exampleInputAmount" placeholder="username" value="">
<div class="input-group-addon">.example.com</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
If jQuery is ok for you, here is a simple solution:
$(function() {
$('#form').submit(function() {
var subdomain = $('#exampleInputAmount').val();
$('#form').attr('action', 'http://' + subdomain + '.example.com');
return true;
});
});
After button clicked the form will be submitted. So on the top of page you may use below code.
PHP Solution:
if(isset($_GET['submit'])){
$text = $_GET['inputtext'];
$url = 'http://'.$text.'example.com';
header('Location: ' . $url);
exit();
}
HTML Change:
<input type="text" name="inputtext" class="form-control" id="exampleInputAmount" placeholder="username" value="">
<button name="submit" type="submit" class="btn btn-primary">Login</button>
Related
I am using a jQuery function which work to hide placeholder text on form focus, this functions works perfectly on the form in main page (Index.php), however it is not working on other forms which are designed using bootstrap (Members.php) as an example. I have tried to see where is the issue but it seems not working. I don't know if version incompatibility of both Bootstrap and jQuery caused this issue?!
I am using
- jquery-3.4.1.min.js
- bootstrap-3.3.7
=========================Backend.js=================================
// here is the jQuery function
$(function(){
'use strict';
// Hide Placeholder On Form Focus
$('[placeholder]').focus(function (){
$(this).attr('data-text',$(this).attr('placeholder'));
$(this).attr('placeholder', '');
}).blur(function (){
$(this).attr('placeholder',$(this).attr('data-text'));
});
});
==============================Index.php=====================================
// Here is the form where the jQuery function works fine
<form class="login" action="<?php echo $_SERVER['PHP_SELF']?>" method = "POST">
<h4 class="text-center">Admin Login</h4>
<input class = "form-control input-lg" type="text" name="user" placeholder="Username" autocomplete="off"/>
<input class = "form-control input-lg" type="password" name="pass" placeholder="Password" autocomplete="new-password"/>
<input class = "btn btn-lg btn-primary btn-block" type="submit" value="Login" />
</form>
===============================Members.php===================================
// Function doesn't work on this form
<div class="container">
<form class="form-horizontal" action="?do=Insert" method="POST">
<!-- start username field -->
<div class="form-group">
<label class="col-sm-2 control-label" >Username</label>
<div class="col-sm-8">
<input type="text" name="username" class="form-control" autocomplete="off" required = "required" placeholder="Enter User Name" />
</div>
</div>
</form>
</div>
var input = $("input");
input.focus(function(){
$(this).attr("placeholder", " ")
});
input.blur(function(){
$(this).attr("placeholder", "example")
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="example">
What I want know that is that when form data is POST to a php service directly from onClick="insert.php" then is it safe. Please explain your answer with details.
for example:
<form role="form" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label>User Id</label>
<input class="form-control" name="First Name">
</div>
<div class="form-group">
<label>City Id</label>
<input class="form-control" name="Last Name">
</div>
<button type="submit" class="btn btn-primary" onclick="allowStory.php">Allow</button><button type="submit" class="btn btn-primary" onclick="insert.php">Insert</button>
</form>
It also return to same page by executing php service as I wanted. But question is that is it safe? and is this type have any drawback? if yes then what are those?
Putting a filename in onclick doesn't do anything, the onclick attribute has to contain Javascript code.
If you want a submit button to go to a specific script instead of the action of the form, use the formaction attribute.
<button type="submit" class="btn btn-primary" formaction="allowStory.php">Allow</button><button type="submit" class="btn btn-primary" formaction="insert.php">Insert</button>
It's perfectly safe to do this, it's no different from specifying the action of the form in the action="scriptname.php" attribute of the <form> tag.
Use following HTML
<form role="form" method="POST" id="formsend" enctype="multipart/form-data">
<div class="form-group">
<label>User Id</label>
<input class="form-control" name="First Name">
</div>
<div class="form-group">
<label>City Id</label>
<input class="form-control" name="Last Name">
</div>
<input type="submit" name="submit" value="Submit" /></form>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(document).on('submit', '#formsend', function()
{
$.post('savedate.php', $(this).serialize(), function(data)
{
$("#sent").html(data);
});
return false;
});
and create a savedata.php or any other name and put your php code to save data in it. you can not give filename to onclick event
I have the following code displaying input fields in a form. I want to have the submit button active only once all fields are filled. I cant seem to figure out where I've gone wrong. I have omitted some text inputs here for space.
Form:
<?php
if(#$_GET['q']==4 && !(#$_GET['step']) ) {
echo '
<div class="row">
<span class="title1" style="margin-left:40%;font-size:30px;"><b>Enter Quiz Details</b></span><br /><br />
<div class="col-md-3"></div><div class="col-md-6"> <form class="form-horizontal title1" name="form" action="update.php?q=addquiz" method="POST">
<fieldset>
<!-- Text input-->
<div class="form-group">
<div class="col-md-12">
<label for="name">Enter Title</label>
<input id="name" name="name" class="form-control input-md" type="text">
</div>
</div>
<div class="form-group">
<label class="col-md-12 control-label" for=""></label>
<div class="col-md-12">
<input type="submit" style="margin-left:45%" class="btn btn-primary" value="Submit" class="btn btn-primary" id="submit" disabled="disabled"/>
</div>
</div>
</fieldset>
</form>
</div>';
}
?>
<script>
(function() {
$('form input').keyup(function() {
var empty = false;
$('form > input').each(function() {
if ($(this).val() == '') {
empty = true;
}
});
if (empty) {
$('#submit').attr('disabled', 'disabled'); //Leave as disabled if any of the fields are empty
} else {
$('#submit').removeAttr('disabled');//Remove the disabled attribute once all fields are filled
}
});
});
</script>
The code is exactly as it appears here. If I've missed something, kindly point me in a direction. Thank you.
You are using the attribute as disabled="disabled" buts its an attribute having no value you should use it like this <input type="submit" style="margin-left:45%" class="btn btn-primary" value="Submit" class="btn btn-primary" id="submit" disabled/>
Here I have created a working JSFiddle for you check it and do correction https://jsfiddle.net/g1cra5f8/
<div class="form-group">
<label for="inputsm">Put URL</label>
<input class="form-control input-sm" id="inputsm" type="text">
<br>
<input type="submit" class="btn btn-info" value="Count Contact">
</div>
This code is my text box and button
<?php
$getText = file_get_contents("", true);
$Contact = substr_count($getText ,"CONTACTID");
print_r($Contact);
?>
and this code is php for get a value and count
Example
I need to put this link https://s3-ap-southeast-1.amazonaws.com/cloudpoc2/us-east-1%3A4502ecdd-1994-40da-8eb2-b6ccc96d6be0/Contacts/Contact_2014_11_19_09_53_28_278.vcf in text box
and click button it show number of contact in this file
$getText = file_get_contents("https://s3-ap-southeast-1.amazonaws.com/cloudpoc2/us-east-1%3A4502ecdd-1994-40da-8eb2-b6ccc96d6be0/Contacts/Contact_2014_11_19_09_53_28_278.vcf", true);
You have to submit your form through POST or GET. My example will use POST. You also must assign a name to the elements you're posting (such as URL). This is untested, so it may have some errors. But it will give you an idea.
<?php
if(isset($_POST['submit'])){
$url = $_POST['url'];
$getText = file_get_contents("$url", true);
$Contact = substr_count($getText ,"CONTACTID");
print_r($Contact);
}
?>
<form method="POST" action="?">
<div class="form-group">
<label for="inputsm">Put URL</label>
<input name="url" class="form-control input-sm" id="inputsm" type="text">
<br>
<input name="submit" type="submit" class="btn btn-info" value="Count Contact">
</div>
</form>
I am building a simple form script that collects a users email and returns a PIN.
The input sits in standard HTML below:
<p>
<form class="form-inline" role="form" method="POST">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="submit" name="srSubmit" class="btn btn-default btn-lg">Generate PIN</button>
</form>
</p>
I have the following if statement that checks the database to see if the email already exists, and if the user would like a PIN reminder.
if($num_rows != 0) {//if email found in table
?>
Email already registered, would you like a PIN reminder?
<form method="POST" action="">
<input type="submit" name="srSend" value="Click here to get pin reminder" />
<input type="hidden" name="srEmail" value="<?php echo strtolower($email);?>" />
</form>
<?php
exit;
}
At the moment, this returns the result to the user as a new page; how do I put this in the actual HTML of the body page, so it would actually appear below the original form input in a new <p> element?
This is a piece of cake with jquery.post
include the jquery library in your html head and you'll need a short script to get the php content by ajax
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function(){
$('#yourForm').submit(function(e){
e.preventDefault();
var data=$('#yourForm').serialize();
$.post('yourphp.php',data,function(html){
$(body).append(html);
});
});
});
</script>
</head>
<body>
<p>
<form id="yourForm" class="form-inline" role="form" method="POST">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="submit" name="srSubmit" class="btn btn-default btn-lg">Generate PIN</button>
</form>
</p>
</body>
You could able to achieve that without ajax too. Simply use a hidden iframe in your main page, then set the target attribute of your form to the iframe as follows:
<form method="post" action="page.php" target="myIframe">
.....
</form>
<p id="theResultP"></p>
<iframe src="#" name="myIframe" style="visibility: hidden"></iframe>
The question now, How could you make the page loaded in the iframe "page.php" to interact with the opener page to update the p. This may be done as follow:
//page.php
<script>
result = parent.document.getElementById('theResultP');
result.innerHtml = "<b>The message you want</b>"
</script>
I dont know if I get what you asking for,but this is my solution :
<p>
<form class="form-inline" role="form" method="POST">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="submit" name="srSubmit" class="btn btn-default btn-lg">Generate PIN</button>
</form>
</p>
<?php
if (isset($message)){
<p><?php print $message ?></p>
?>
<?php
}
?>
and in top of your file write something like this :
<?php
if($_post['srSend']){
$message='write your message here';
}
?>