How to send form values to a backend Flask? [duplicate] - ajax

This question already has answers here:
Sending data from HTML form to a Python script in Flask
(2 answers)
Get the data received in a Flask request
(23 answers)
How to get POSTed JSON in Flask?
(13 answers)
jQuery posting JSON
(3 answers)
Submit a form using jQuery [closed]
(22 answers)
Closed 5 years ago.
I have heard that we can do this with ajax for it, but how do we achieve it? Please give a brief explanation of it.

Example Form:
<form role="form" action="http://127.0.0.1:8080/user/login" method="post" name="loginform">
<fieldset>
<legend>Login</legend>
<div class="form-group">
<label for="email">Email</label>
<input type="text" name="email" placeholder="Your Email" required class="form-control" />
</div>
<div class="form-group">
<label for="name">Password</label>
<input type="password" name="password" placeholder="Your Password" required class="form-control" />
</div>
<div class="form-group">
<input type="submit" name="login" value="Login" class="btn btn-primary" />
</div>
</fieldset>
</form><form role="form" action="http://127.0.0.1:8080/user/login" method="post" name="loginform">
<fieldset>
<legend>Login</legend>
<div class="form-group">
<label for="email">Email</label>
<input type="text" name="email" placeholder="Your Email" required class="form-control" />
</div>
<div class="form-group">
<label for="name">Password</label>
<input type="password" name="password" placeholder="Your Password" required class="form-control" />
</div>
<div class="form-group">
<input type="submit" name="login" value="Login" class="btn btn-primary" />
</div>
</fieldset>
</form>
If you want to use Javascript ajax call from front-end to back-end:
$.ajax({
method: 'post',
url: '/login',
success: function(res) {
return 'success';
}
})
tangle-make:
#SHELL := /bin/bash
BUILD_DIR=build
VER_BRANCH=build-release
VER_FILE=VERSION
ORG_MODE_DIR=~/emacs/lisp
LITERATE_TOOLS="https://github.com/vlead/literate-tools.git"
LITERATE_DIR=literate-tools
ELISP_DIR=elisp
ORG_DIR=org-templates
STYLE_DIR=style
CODE_DIR=build/code
DOC_DIR=build/docs
SRC_DIR=src
PWD=$(shell pwd)
STATUS=0
all: check-org build
check-org:
ifeq ($(wildcard ${ORG_MODE_DIR}/org-8.2.10/*),)
mkdir -p ${ORG_MODE_DIR}
wget http://orgmode.org/org-8.2.10.tar.gz
tar zxvf org-8.2.10.tar.gz
rm -rf org-8.2.10.tar.gz
mv org-8.2.10 ${ORG_MODE_DIR}
else
#echo "org-mode org-8.2.10 already present"
endif
build: init write-version
emacs --script elisp/publish.el
init: mk-symlinks
rm -rf ${BUILD_DIR}
mkdir -p ${BUILD_DIR} ${CODE_DIR}
mk-symlinks: pull-literate-tools
(ln -sf ${LITERATE_DIR}/${ELISP_DIR}; \
ln -sf ../${LITERATE_DIR}/${ORG_DIR} ${SRC_DIR}; \
ln -sf ../${LITERATE_DIR}/${STYLE_DIR} ${SRC_DIR})
pull-literate-tools:
#echo "checking for literate support ..."
echo "pwd=..."
echo ${PWD}
ifeq ($(wildcard ${LITERATE_DIR}),)
#echo "proxy is..."
echo $$http_proxy
(git clone ${LITERATE_TOOLS})
else
#echo "Literate support code already present"
endif
# variable that will exist of git command exists
# solution from: http://stackoverflow.com/questions/5618615/check-if-a-program-exists-from-a-makefile
GIT_EXISTS := $(shell command -v git 2> /dev/null)
# get the latest commit hash and its subject line
# and write that to the VERSION file
write-version:
ifdef GIT_EXISTS
# allow these to fail since the parent folder may not have a git repo.
echo -n "Built from commit: " > ${CODE_DIR}/${VER_FILE}
- echo `git rev-parse HEAD` >> ${CODE_DIR}/${VER_FILE}
- echo `git log --pretty=format:'%s' -n 1` >> ${CODE_DIR}/${VER_FILE}
endif
clean-literate:
rm -rf ${ELISP_DIR}
rm -rf src/${ORG_DIR}
rm -rf src/${STYLE_DIR}
rm -rf ${LITERATE_DIR}
clean: clean-literate
rm -rf ${BUILD_DIR}

Related

How to put route url in the echo in laravel form inside a controller?

This is the code inside the controller of my laravel:
echo '
<form action="'.$link.'" method="POST">
';
csrf_token();
echo '
<div class="form-group">
<label for="reason">Total Worked Hours::</label>
<input type="text" class="form-control" disabled="disabled" id="hours" value="'.$totalhours.'"style="color:red;font-weight:bold;"">
</div>
<div class="form-group">
<label for="reason">Enter Your Reason:</label>
<input type="text" class="form-control" id="text">
</div>
<button type="submit" class="btn btn-default">Submit Reason</button>
</form>
';
and the error is 419 page expired what is the proper code for this.
change the line
csrf_token();
by
echo csrf_field();
Or, depending on your ajax, remove it completly.

I can't get back()->withInput() to work, the documentation seems a little sparse on how it should work

I'm using blade templates and this is how my form looks:
<form action="{{ route("user-sessions.store") }}" method="POST">
#csrf
<div class="form-group">
<label for="e-mail">E-mail Address</label>
<input name="email_address" class="form-control" type="email" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input name="password" class="form-control" type="password" />
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" />
</div>
</form>
Does back()->withInput() only work with {{ Form::open() }}? If so, the 7.x documentation doesn't say that it seems. I would think if you need a 3rd party library to get this to work, the documentation should say so. If that's not the issue, then what am I doing wrong?
I'm using Laravel 7.x.
if you use html collective, it automatically puts old values in form inputs. But when use pure html to create form inputs you have to use old method for input values. like this:
<form action="{{ route("user-sessions.store") }}" method="POST">
#csrf
<div class="form-group">
<label for="e-mail">E-mail Address</label>
<input name="email_address" class="form-control" type="email" value="{{old('email_address')}}"/>
</div>
<div class="form-group">
<label for="password">Password</label>
<input name="password" class="form-control" type="password" value="{{old('password')}}"/>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" />
</div>
</form>

Login and keep session cookie open with wget

Based on checking the man pages and examples online, i use the following to goto a page with a login screen, passing in the credentials.
wget --no-check-certificate --save-cookies cookies.txt --keep-session-cookies --post-data 'userID=user1&password=password1' https://x.y.com/service.php
wget --no-check-certificate --load-cookies cookies.txt https://x.y.com/service.php
The service.php login page's returned file shows that the fields for user and password above are correct (userID and password).
<div style="width: 435px;margin-bottom: 20px;height: 29px;">
<label style="line-height: 30px;float:left" for="usermail">User ID:</label>
<input style="float:right;font-size: 9pt;width: 350px;margin-right: 0px;" type="text" name="userID" required>
</div>
<div style="width: 435px;margin-bottom: 10px;height: 29px;">
<label style="line-height: 30px;float:left" for="password">Password:</label>
<input style="float:right;font-size: 9pt;width: 350px;margin-right: 0px;" type="password" name="password" required>
</div>
However after running the wget with load-cookies, we still get the page returned with the login page. I'm sure it's something small i'm doing incorrectly, but am not sure.

How to find and replace block of codes in bash on multiple files on multiple folder

Hi have this dilemma that where i have multiple blocks of code in multiple files and in multiple folder now i wanted to make change in one go.
Heres the code in bash
#! /bin/bash
findcode="<form name="activateform" id="activateform" method="post" action="<?php echo network_site_url('wp-activate.php'); ?>">
<p>
<label for="key"><?php _e('Activation Key:') ?></label>
<br /><input type="text" name="key" id="key" value="" size="50" />
</p>
<p class="submit">
<input id="submit" type="submit" name="Submit" class="submit" value="<?php esc_attr_e('Activate') ?>" />
</p>
</form>"
replacecode="<?php if ( !defined( 'ABSPATH' ) ) { exit } ?>"
for grep -lr $findcode * do
sed -i 's/$findcode/replacecode/g' folders and subfolders
done
Basically the idea is
blocCodeToBeReplace = <?php block of code here ?>
blockCodeToReplace = <?php block of code here to replace including code ?>
find $blocCodeToBeReplace on all .php files on each folder and subfolder then replace $blockCodeToReplace.

Codeigniter change post variable

Im sending a post form on codeigniter, but when I receive it change the string.
The string is: %1001200040010_
it cames from a plastic card.
And the echo shows: 01200040010_
I have tried with php without codeigniter and works fine.
Could you help me?
Te form is
<div id="cl-wrapper">
<div class="container-fluid" id="pcont">
<div class="cl-mcont">
<form action="testpost" method="post">
<p><strong>Introduzca el Nro. de tarjeta del socio:</strong><?php echo validation_errors(); ?><br />
<input name="socio" maxlength="20" value="" size="20" autocomplete="off"/>
</p>
<div><input type="submit" value="Enviar" class="btn btn-primary" /></div>
</form>
</div>
</div>
</div>
the php
function testpost()
{
echo utf8_decode($_POST['socio']);
exit();
}
Thanks a lot!.
I have solved my problem.
I receive the variable post with $_REQUEST instead $_POST. Like this:
utf8_decode($_REQUEST['variable']) ;
Thanks...

Resources