Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138221

How to fire two functions on single button click

$
0
0

Hello stackoverflow community I am having problem in using onpress method. Actually i want to fire two functions on a single button click like if i press Register button the validate() function and userRegister function works first validate function validate all the fields and then register function registers user into db. I seen setState is responsible for this kind of behaviour since i m new in react native development so i cant implement this kind of functionality my code :

import React, { Component } from 'react';

import {ToastAndroid, StyleSheet, View, TextInput, Button, Text, Alert } from 'react-native';

export default class Project extends Component {

constructor(props) {

    super(props)

    this.state = {

      UserName: '',
      UserEmail: '',
      UserPassword: '',
      cpassword: "",
      UserCity: ''

    };

  }

  Validate=()=>{
    if(this.state.UserName == ""){
      ToastAndroid.show('Enter UserName',ToastAndroid.SHORT)
    }
    else if(this.state.UserEmail == ""){
      ToastAndroid.show('Enter Email',ToastAndroid.SHORT)
    }
    else if(this.state.UserPassword == ""){
      ToastAndroid.show('Enter Password',ToastAndroid.SHORT)
    }
    else if(this.state.cpassword == ""){
      ToastAndroid.show('Enter Confirm Password',ToastAndroid.SHORT)
    }
    else if (this.state.UserPassword != this.state.cpassword){
      ToastAndroid.show('Password did not match',ToastAndroid.SHORT)
    }
    else if(this.state.UserCity == ""){
      ToastAndroid.show('Enter City Name',ToastAndroid.SHORT)
    }
    else {
      ToastAndroid.show('User Registration Sucessfull', ToastAndroid.SHORT)
    }
    console.log(this.state)
  }

UserRegistrationFunction = () =>{
    const {UserName} = this.state;
    const {UserEmail} = this.state;
    const {UserPassword} = this.state;
    const {UserCity} = this.state;


  fetch('http://192.168.0.107/loginrn/user_registration.php', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',

    },

    body: JSON.stringify({

      name: this.state.UserName,


      email: this.state.UserEmail,

      password: this.state.UserPassword,

      //confpassword: this.state.cpassword
      city : this.state.UserCity,


    })
  }).then((response) => response.json())
        .then((responseJson) => {


  // Showing response message coming from server after inserting records.

        Alert.alert(responseJson);
        }).catch((error) => {
          console.error(error);
        });

}

  render() {

    return (

<View style={styles.MainContainer}>

        <Text style= {styles.title}>User Registration Form</Text>

        <TextInput
          placeholder="Enter User Name"
          onChangeText={name => this.setState({UserName : name})}
          underlineColorAndroid='transparent'
          style={styles.TextInputStyleClass}
          />

        <TextInput
          placeholder="Enter User Email"
          onChangeText={email => this.setState({UserEmail : email})}
          underlineColorAndroid='transparent'
          style={styles.TextInputStyleClass}
          />

        <TextInput
          placeholder="Enter User Password"
          onChangeText={password => this.setState({UserPassword : password})}
          underlineColorAndroid='transparent'
          style={styles.TextInputStyleClass}
          //secureTextEntry={true}
          />
           <TextInput
          placeholder="Enter User Confirm Password"
          onChangeText={cpassword => this.setState({cpassword})}
          underlineColorAndroid='transparent'
          style={styles.TextInputStyleClass}
          //secureTextEntry={true}
          />

        <TextInput
          placeholder="Enter User City Name"
          onChangeText={city => this.setState({UserCity : city})}
          underlineColorAndroid='transparent'
          style={styles.TextInputStyleClass}
          //secureTextEntry={true}
          />
        <Button title="Click Here To Register"  
        onPress={this.Validate}
        color="#2196F3" />
        <Button title = "Next" onPress={this.UserRegistrationFunction} color = "#2196F3"/>



</View>

    );
  }
}

Viewing all articles
Browse latest Browse all 138221

Trending Articles