Hiding php code from URL - javascript

Is there I can hide the PHP information in the URL without reloading the page?
I have the current HTML code:
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="get">
<label>
<input pattern=".{3,}" required title="3 characters minimum" name="url" class="urlinput" type="text">
</label>
<input name="submit" class="urlsub" type="submit">
</form>
And when I click submit this happens
http://example.com/?url=example&submit=Submit
Is there any way I can keep it as example.com while the information still passes to php?
Full code:
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<label>
<input pattern=".{3,}" required title="3 characters minimum" name="url" class="urlinput" type="text">
</label>
<input name="submit" class="urlsub" type="submit">
</form>
<?php
IF($_POST['submit']) {
include('include/functions.php');
// IP OF THE USER
$ip = $_SERVER['REMOTE_ADDR'];
// URL TO PUT IN Database
$url = $_POST['url'];
// MAKE SIMPLE URL EG: HTTPS://WWW.GOOGLE.COM -> GOOGLE.COM
$surl = surl($url);
// RUN SCRIPT
CIURLE($surl, $ip);
}
?>

Change from using GET method to using POST method
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<label>
<input pattern=".{3,}" required title="3 characters minimum" name="url" class="urlinput" type="text">
</label>
<input name="submit" class="urlsub" type="submit">
</form>
<?php
if($_POST['submit']) {
echo 'Posted url: '.$_POST['url'] . '<br>';
echo 'Remote IP:'.$_SERVER['REMOTE_ADDR'] . '<br>';
}
?>

Use "POST" instead of "GET". It's explained in the link.
http://www.w3schools.com/Php/php_forms.asp

Related

posting form data from iframe src

login.php
<iframe id ="login_iframe" name="login_iframe" src ="iframe.php"></iframe>
and in my iframe.php
<html>
<head>
</head>
<body>
<form id="login_submitform" method="post" action="post.php" target="_top" />
<input type="text" name="customer" value=""/>
<input type = "text" name="phone" value=""/>
<input type= "submit" value="submit" />
and in my post.php comes the result
$customer = $_POST['customer'];
$phone = $_POST['phone'];
?>
RESULT
customer = empty string
phone= empty string
what am i doing wrong here? how can i get the form values to post.php. pls assist
Everything looks fine, things to be noted here :
You can only access data of iframe in same domain, your login.php, iframe.php and post.php must be in same domain, here assuming you are testing on localhost
please restrict your form fields, with required. you are good to go.
<form id="login_submitform" method="post" action="post.php" target="_top" />
Customer: <input type="text" name="customer" value="" required/>
Phone: <input type = "text" name="phone" value="" required/>
<input type= "submit" value="submit" />
</form>
It will not submit if value are empty.
post.php
<?php
echo '<pre>';
print_r($_POST);
echo $_POST['customer'] .'<br>';
echo $_POST['phone'] .'<br>'
?>

how to prin a message in screen and then redirect to other page php/js

i have a form and i connected with php to a database.It works perfect but i 'd like to show me o pop up message that is succesfull login .
<form id = "quiz" name="quiz" method="post" action="insert.php" >
name: <input id="name" type="text" name="name" placeholder="π.χ. George" required="">
<label for="name"> </label>
faculty: <input type="text" name="faculty" placeholder="Enter your Faculty" required="">
<input class="btn btn-one" type="submit" value="Submit" />
</form>
insert.php is the php file that connect the form fields with the database.
then try this in this file:
echo "<script>alert('message successfully sent');</script>";
header('Location: main.html');
$stmt->close();
$conn->close();
but i get an error.If i delete echo "alert('message successfully sent');"; works perfect but i would like to have a message that's successfull.
This will work
echo "<script>alert('message successfully sent');
window.location.href='main.html'
</script>";

how to give name in auto submit form javascript

This is my code that I am using to submit form with post value
<form action="<?php echo DOMAIN; ?>contact/booking-form.php" method="post">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="text" name="email" value="<?php echo $email; ?>" />
<input type="submit" name="submit" id="submit" />
<script>document.getElementById('submit').submit();</script>
</form>
Can anybody help me to pass name="submit" value of submit button to another page?
A submit button is only going to be a successful control if it is used to submit the form (and even then only if it has a name and a value … which yours does not).
If you want submit=submit in your form data when you submit the form with JavaScript, then don't use a submit button to put that data in the form in the first place. Use a hidden input.
<input type="submit">
<input type="hidden" name="submit" id="submit" value="submit">
Then you have two other problems.
First, submit is a method of form elements, not inputs. So you need to change your script to call the right element.
<script>document.getElementById('submit').form.submit();</script>
Second, if a form has a control called submit then that will clobber the submit method. So you need to get one from a different form (not supported in old versions of Internet Explorer):
<script>
var form = document.getElementById('submit').form;
var submit_method = document.createElement("form").submit;
submit_method.call(form);
</script>
<form id=submit action="<?php echo DOMAIN; ?>contact/booking-form.php" name="form1" method="post">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="text" name="email" value="<?php echo $email; ?>" />
</form>
<script >
document.form1.submit()
</script>
there is no sumit button in your code. first add in html
<input type="submit" value="submit" id="submit"/>
<script>
document.getElementById("submit").value = "newSubmitButtonValue";
</script>

Automatically add a certain class, whenever <Form> code contains a certain value?

I want to add a special class to each form that contains the word special as part of its action URL.
But I have many forms, and I can't think of an automated way to do this.
The only way I came up with was to create the following code, but I will have to create such code for each and every form, using a different index number:
<?php
$case1 = "special";
$case2 = "none";
if($case1 == "none") {
$class1 = ""; // don't add any class
}
else {
$class1 = "effect"; // add `effect` class
}
if($case2 == "none") {
$class2 = ""; // same goes here
}
else {
$class2 = "effect"; // and here
}
?>
HTML:
<form action="/go/<?= $case1 ?>" method="POST" target="_blank">
<input type="submit" class="<?= $class1 ?> general-class" value="submit">
</form>
<form action="/go/<?= $case2 ?>" method="POST" target="_blank">
<input type="submit" class="<?= $class2 ?> general-class" value="submit">
</form>
OUTPUT:
<form action="/go/special" method="POST" target="_blank">
<input type="submit" class="effect general-class" value="submit">
</form>
<form action="/go/none" method="POST" target="_blank">
<input type="submit" class="general-class" value="submit">
</form>
Is there any automated way to do this?
Basically I have two types of forms, one should be opened using a jquery plugin (henace the special class), and the other should open in a normal way.
My way to differentiate between them is to insert the $case[i] variable into the action url, as this is something that I have to do either way.
Perhaps there's a complete different way to achieve this, I don't know.
EDIT:
Real Form is generated mostly manually, with this code:
<form action="/go/<?= $item ?>/<?php echo $case1 ; ?>" method="POST" target="_blank">
<input name="a" type="hidden" value="<?php echo $a; ?>"/>
<input name="b" type="hidden" value="<?php echo $b; ?>"/>
<input name="c" type="hidden" value="<?php echo $c; ?>"/>
<input name="d" type="hidden" value="<?php echo $d; ?>"/>
<input type="submit" class="<?= $class1 ?> general-class" value="Click Me"></form>
(all variables are being given values at the start of the PHP block you see above)
There's two ways - PHP and jQuery. Based on your code, we don't have enough info to know if we can use the PHP way or not, so here's the jQuery way:
// wait until the document is ready
jQuery(function($) {
// find all forms that have "special" in the action
$('form[action*="special"]').each(
function() {
// within the form, find the submit button, and add the "effect" class
$(this).find('input[type="submit"]').addClass('effect');
}
);
});
And, the shorter / less verbose way:
jQuery(function($) {
// find all forms that have "special" in the action, find their input, and add the class
$('form[action*="special"] input[type="submit"]').addClass('effect');
});
The PHP Way
So, to do this in PHP, I would recommend writing a simple function, then calling it.
function get_class( $slug ) {
$class_map = array(
'special' => 'effect',
'none' => '',
// .. you could add others here if appropriate
return ( isset( $class_map[ $slug ] ) ) ? $class_map[ $slug ] : '';
);
Then you could use it in your php like so:
<form action="/go/<?= $item ?>/<?php echo $case1 ; ?>" method="POST" target="_blank">
<input name="a" type="hidden" value="<?php echo $a; ?>"/>
<input name="b" type="hidden" value="<?php echo $b; ?>"/>
<input name="c" type="hidden" value="<?php echo $c; ?>"/>
<input name="d" type="hidden" value="<?php echo $d; ?>"/>
<input type="submit" class="<?= get_class( $case1 ); ?> general-class" value="Click Me"></form>
While that may not seem like a big value, if you started applying those concepts to your code, you would quickly see the value start adding up.

PHP_SELF & Javascript setAttribute

I have the following code in PHP:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php $value = $_POST['Field1'] ?>
<input type="text" id="Field1" name="Field1" value="<?php echo $value ?>">
<input type="button" value="ButtonValue" onclick="SubmitFunction()">
</form>
And some function in JavaScript:
<script>
function SubmitFunction(){
//... do some stuff here
//But here I need to assign value to Field1:
document.getElementById("Field1").setAttribute("value", "Some value");
document.form.submit();
}
</script>
When this code loads for the first time, it says there is undefined index Field1. It's absolutely right.
But when I set attribute value to this field using SubmitFunction(), page reloads and it still can't find Field1 ! This is my problem.
I noticed that in pure HTML code the initial form looks like:
<form>
<input type="text" id="Field1" name="Field1" value>
<input type="button" value="ButtonValue" onclick="SubmitFunction()">
</form>
Where is my problem (except brain)?
<script>
function SubmitFunction(){
//... do some stuff here
//But here I need to assign value to Field1:
document.getElementById("Field1").setAttribute("value", "Some value");
document.form.submit(document.getElementById("Field1").getAttribute("value"));
}
</script>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php $value = $_POST['Field1'] ?>
<input type="text" id="Field1" name="Field1" value="<?php echo $value?$value:''; ?>">
<input type="button" value="ButtonValue" onclick="SubmitFunction()">
</form>

Categories

Resources